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); |
|
#ifdef VBV |
|
273 |
static int check_curve_for_vbv_compliancy(rc_2pass2_t * rc, const float fps); |
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); |
static int scale_curve_for_vbv_compliancy(rc_2pass2_t * rc, const float fps); |
|
#endif |
|
|
|
|
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 |
290 |
if (rc == NULL) |
if (rc == NULL) |
291 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
292 |
|
|
293 |
rc->param = *param; |
/* v1.0.x */ |
294 |
|
rc->param.version = param->version; |
295 |
|
rc->param.bitrate = param->bitrate; |
296 |
|
rc->param.filename = param->filename; |
297 |
|
rc->param.keyframe_boost = param->keyframe_boost; |
298 |
|
rc->param.curve_compression_high = param->curve_compression_high; |
299 |
|
rc->param.curve_compression_low = param->curve_compression_low; |
300 |
|
rc->param.overflow_control_strength = param->overflow_control_strength; |
301 |
|
rc->param.max_overflow_improvement = param->max_overflow_improvement; |
302 |
|
rc->param.max_overflow_degradation = param->max_overflow_degradation; |
303 |
|
rc->param.kfreduction = param->kfreduction; |
304 |
|
rc->param.kfthreshold = param->kfthreshold; |
305 |
|
rc->param.container_frame_overhead = param->container_frame_overhead; |
306 |
|
|
307 |
|
if (XVID_VERSION_MINOR(param->version) >= 1) { |
308 |
|
rc->param.vbv_size = param->vbv_size; |
309 |
|
rc->param.vbv_initial = param->vbv_initial; |
310 |
|
rc->param.vbv_maxrate = param->vbv_maxrate; |
311 |
|
rc->param.vbv_peakrate = param->vbv_peakrate; |
312 |
|
}else{ |
313 |
|
rc->param.vbv_size = |
314 |
|
rc->param.vbv_initial = |
315 |
|
rc->param.vbv_maxrate = |
316 |
|
rc->param.vbv_peakrate = 0; |
317 |
|
} |
318 |
|
|
319 |
/* Initialize all defaults */ |
/* Initialize all defaults */ |
320 |
#define _INIT(a, b) if((a) <= 0) (a) = (b) |
#define _INIT(a, b) if((a) <= 0) (a) = (b) |
452 |
* shape the curve in the BEFORE/AFTER pair of functions */ |
* shape the curve in the BEFORE/AFTER pair of functions */ |
453 |
scaled_curve_apply_advanced_parameters(rc); |
scaled_curve_apply_advanced_parameters(rc); |
454 |
|
|
|
|
|
|
#ifdef VBV |
|
455 |
/* Check curve for VBV compliancy and rescale if necessary */ |
/* Check curve for VBV compliancy and rescale if necessary */ |
456 |
|
|
|
|
|
457 |
#ifdef VBV_FORCE |
#ifdef VBV_FORCE |
458 |
if (rc->param.vbvsize==0) |
if (rc->param.vbv_size==0) |
459 |
{ |
{ |
460 |
rc->param.vbvsize = 3145728; |
rc->param.vbv_size = 3145728; |
461 |
rc->param.vbvinitial = 2359296; |
rc->param.vbv_initial = 2359296; |
462 |
rc->param.vbv_maxrate = 4000000; |
rc->param.vbv_maxrate = 4000000; |
463 |
rc->param.vbv_peakrate = 10000000; |
rc->param.vbv_peakrate = 10000000; |
464 |
} |
} |
465 |
#endif |
#endif |
466 |
|
|
467 |
if (rc->param.vbvsize>0) /* vbvsize==0 switches VBV check off */ |
if (rc->param.vbv_size>0) /* vbv_size==0 switches VBV check off */ |
468 |
{ |
{ |
469 |
const double fps = (double)create->fbase/(double)create->fincr; |
const double fps = (double)create->fbase/(double)create->fincr; |
470 |
int status = check_curve_for_vbv_compliancy(rc, fps); |
int status = check_curve_for_vbv_compliancy(rc, fps); |
482 |
fprintf(stderr,"impossible.\n"); |
fprintf(stderr,"impossible.\n"); |
483 |
#endif |
#endif |
484 |
} |
} |
|
#endif |
|
|
|
|
485 |
*handle = rc; |
*handle = rc; |
486 |
return(0); |
return(0); |
487 |
} |
} |
1436 |
return; |
return; |
1437 |
} |
} |
1438 |
|
|
|
|
|
|
#ifdef VBV |
|
|
|
|
1439 |
/***************************************************************************** |
/***************************************************************************** |
1440 |
* VBV compliancy check and scale |
* VBV compliancy check and scale |
1441 |
* MPEG-4 standard specifies certain restrictions for bitrate/framesize in VBR |
* MPEG-4 standard specifies certain restrictions for bitrate/framesize in VBR |
1457 |
|
|
1458 |
static int check_curve_for_vbv_compliancy(rc_2pass2_t * rc, const float fps) |
static int check_curve_for_vbv_compliancy(rc_2pass2_t * rc, const float fps) |
1459 |
{ |
{ |
1460 |
/* We do all calculations in float, for higher accuracy, and bytes for convenience |
/* We do all calculations in float, for higher accuracy, |
1461 |
|
and in bytes for convenience |
1462 |
|
|
1463 |
typical values from DivX Home Theater profile: |
typical values from DivX Home Theater profile: |
1464 |
vbvsize= 384*1024 (384kB), vbvinitial= 288*1024 (75% fill) |
vbv_size= 384*1024 (384kB), vbv_initial= 288*1024 (75% fill) |
1465 |
maxrate= 4000000 (4MBps), peakrate= 10000000 (10MBps) |
maxrate= 4000000 (4MBps), peakrate= 10000000 (10MBps) |
1466 |
|
|
1467 |
PAL: offset3s = 75 (3 seconds of 25fps) |
PAL: offset3s = 75 (3 seconds of 25fps) |
1468 |
NTSC: offset3s = 90 (3 seconds of 29.97fps) or 72 (3 seconds of 23.976fps) |
NTSC: offset3s = 90 (3 seconds of 29.97fps) or 72 (3 seconds of 23.976fps) |
1469 |
*/ |
*/ |
1470 |
|
|
1471 |
const float vbvsize = (float)rc->param.vbvsize/8.f; |
const float vbv_size = (float)rc->param.vbv_size/8.f; |
1472 |
float vbvfill = (float)rc->param.vbvinitial/8.f; |
float vbvfill = (float)rc->param.vbv_initial/8.f; |
1473 |
|
|
1474 |
const float maxrate = (float)rc->param.vbv_maxrate; |
const float maxrate = (float)rc->param.vbv_maxrate; |
1475 |
const float peakrate = (float)rc->param.vbv_peakrate; |
const float peakrate = (float)rc->param.vbv_peakrate; |
1494 |
vbvfill += r0 - rc->stats[i].scaled_length; |
vbvfill += r0 - rc->stats[i].scaled_length; |
1495 |
|
|
1496 |
/* this check is _NOT_ an "overflow"! only reading from disk stops then */ |
/* this check is _NOT_ an "overflow"! only reading from disk stops then */ |
1497 |
if (vbvfill > vbvsize) |
if (vbvfill > vbv_size) |
1498 |
vbvfill = vbvsize; |
vbvfill = vbv_size; |
1499 |
|
|
1500 |
/* but THIS would be an underflow. report it! */ |
/* but THIS would be an underflow. report it! */ |
1501 |
if (vbvfill < 0) |
if (vbvfill < 0) |
1504 |
|
|
1505 |
return VBV_COMPLIANT; |
return VBV_COMPLIANT; |
1506 |
} |
} |
1507 |
/* idea: min(vbvfill) could be stored to print "minimum buffer fill" */ |
/* TODO: store min(vbvfill) and print "minimum buffer fill" */ |
|
|
|
1508 |
|
|
1509 |
|
|
1510 |
static int scale_curve_for_vbv_compliancy(rc_2pass2_t * rc, const float fps) |
static int scale_curve_for_vbv_compliancy(rc_2pass2_t * rc, const float fps) |
1524 |
For each scene of len N, we have to check up to N^2 possible buffer fills. |
For each scene of len N, we have to check up to N^2 possible buffer fills. |
1525 |
This works well with MPEG-2 where N==12 or so, but for MPEG-4 it's a |
This works well with MPEG-2 where N==12 or so, but for MPEG-4 it's a |
1526 |
little slow... |
little slow... |
1527 |
|
|
1528 |
|
TODO: Better control on VBVfill between scenes |
1529 |
*/ |
*/ |
1530 |
const float vbvsize = (float)rc->param.vbvsize/8.f; |
|
1531 |
const float vbvinitial = (float)rc->param.vbvinitial/8.f; |
const float vbv_size = (float)rc->param.vbv_size/8.f; |
1532 |
|
const float vbv_initial = (float)rc->param.vbv_initial/8.f; |
1533 |
|
|
1534 |
const float maxrate = 0.9*rc->param.vbv_maxrate; |
const float maxrate = 0.9*rc->param.vbv_maxrate; |
1535 |
const float vbvlow = 0.10f*vbvsize; |
const float vbv_low = 0.10f*vbv_size; |
1536 |
const float r0 = (int)(maxrate/fps+0.5)/8.f; |
const float r0 = (int)(maxrate/fps+0.5)/8.f; |
1537 |
|
|
1538 |
int i,k,l,n,violation = 0; |
int i,k,l,n,violation = 0; |
1544 |
we could get all this from existing keyframe_positions, somehow, but there we |
we could get all this from existing keyframe_positions, somehow, but there we |
1545 |
don't have a min_scenelength, and it's no big deal to get it again. */ |
don't have a min_scenelength, and it's no big deal to get it again. */ |
1546 |
|
|
1547 |
const int min_scenelength = 50; |
const int min_scenelength = (int)(fps+0.5); |
1548 |
int num_scenes = 0; |
int num_scenes = 0; |
1549 |
int last_scene = -999; |
int last_scene = -999; |
1550 |
for (i=0; i<rc->num_frames; i++) { |
for (i=0; i<rc->num_frames; i++) { |
1606 |
|
|
1607 |
k=0; |
k=0; |
1608 |
Skn = S0n; |
Skn = S0n; |
1609 |
f = (R(k,n-1) + (vbvinitial - vbvlow)) / Skn; |
f = (R(k,n-1) + (vbv_initial - vbv_low)) / Skn; |
1610 |
if (f < minf) |
if (f < minf) |
1611 |
minf = f; |
minf = f; |
1612 |
|
|
1614 |
{ |
{ |
1615 |
Skn -= frames[k].scaled_length; |
Skn -= frames[k].scaled_length; |
1616 |
|
|
1617 |
f = (R(k,n-1) + (vbvsize - vbvlow)) / Skn; |
f = (R(k,n-1) + (vbv_size - vbv_low)) / Skn; |
1618 |
if (f < minf) |
if (f < minf) |
1619 |
minf = f; |
minf = f; |
1620 |
} |
} |
1621 |
} |
} |
1622 |
|
|
1623 |
/* special case: at the end, fill buffer up to vbvinitial again |
/* special case: at the end, fill buffer up to vbv_initial again |
1624 |
TODO: Allow other values for buffer fill between scenes |
TODO: Allow other values for buffer fill between scenes |
1625 |
e.g. if n=N is smallest f-value, then check for better value */ |
e.g. if n=N is smallest f-value, then check for better value */ |
1626 |
|
|
1635 |
{ |
{ |
1636 |
Skn -= frames[k].scaled_length; |
Skn -= frames[k].scaled_length; |
1637 |
|
|
1638 |
f = (R(k,n-1) + (vbvinitial - vbvlow)) / Skn; |
f = (R(k,n-1) + (vbv_initial - vbv_low)) / Skn; |
1639 |
if (f < minf) |
if (f < minf) |
1640 |
minf = f; |
minf = f; |
1641 |
} |
} |
1655 |
{ |
{ |
1656 |
float S_red = 0.f; /* how much to redistribute */ |
float S_red = 0.f; /* how much to redistribute */ |
1657 |
float S_elig = 0.f; /* sum of bit for those scenes you can still swallow something*/ |
float S_elig = 0.f; /* sum of bit for those scenes you can still swallow something*/ |
1658 |
|
float f_red; |
1659 |
int l; |
int l; |
1660 |
|
|
1661 |
for (l=0;l<num_scenes;l++) /* check how much is wrong */ |
for (l=0;l<num_scenes;l++) /* check how much is wrong */ |
1698 |
return -2; |
return -2; |
1699 |
} |
} |
1700 |
|
|
1701 |
const float f_red = (1.f + S_red/S_elig); |
f_red = (1.f + S_red/S_elig); |
1702 |
|
|
1703 |
#ifdef VBV_DEBUG |
#ifdef VBV_DEBUG |
1704 |
printf("Moving %.0f kB to avoid buffer underflow, correction factor: %.5f\n",S_red/1024.f,f_red); |
printf("Moving %.0f kB to avoid buffer underflow, correction factor: %.5f\n",S_red/1024.f,f_red); |
1735 |
} |
} |
1736 |
|
|
1737 |
|
|
|
#endif |
|
|
|
|
|
|
|
1738 |
/***************************************************************************** |
/***************************************************************************** |
1739 |
* Still more low level stuff (nothing to do with stats treatment) |
* Still more low level stuff (nothing to do with stats treatment) |
1740 |
****************************************************************************/ |
****************************************************************************/ |