29 |
* |
* |
30 |
*****************************************************************************/ |
*****************************************************************************/ |
31 |
|
|
32 |
|
#define BQUANT_PRESCALE |
33 |
#undef COMPENSATE_FORMULA |
#undef COMPENSATE_FORMULA |
34 |
|
|
35 |
|
/* forces second pass not to be bigger than first */ |
36 |
|
#undef PASS_SMALLER |
37 |
|
|
38 |
#include <stdio.h> |
#include <stdio.h> |
39 |
#include <math.h> |
#include <math.h> |
40 |
#include <limits.h> |
#include <limits.h> |
43 |
#include "../image/image.h" |
#include "../image/image.h" |
44 |
|
|
45 |
/***************************************************************************** |
/***************************************************************************** |
46 |
* Some constants |
* Some default settings |
47 |
****************************************************************************/ |
****************************************************************************/ |
48 |
|
|
49 |
#define DEFAULT_KEYFRAME_BOOST 0 |
#define DEFAULT_KEYFRAME_BOOST 0 |
50 |
#define DEFAULT_OVERFLOW_CONTROL_STRENGTH 10 |
#define DEFAULT_OVERFLOW_CONTROL_STRENGTH 10 |
51 |
#define DEFAULT_CURVE_COMPRESSION_HIGH 0 |
#define DEFAULT_CURVE_COMPRESSION_HIGH 0 |
52 |
#define DEFAULT_CURVE_COMPRESSION_LOW 0 |
#define DEFAULT_CURVE_COMPRESSION_LOW 0 |
53 |
#define DEFAULT_MAX_OVERFLOW_IMPROVEMENT 60 |
#define DEFAULT_MAX_OVERFLOW_IMPROVEMENT 10 |
54 |
#define DEFAULT_MAX_OVERFLOW_DEGRADATION 60 |
#define DEFAULT_MAX_OVERFLOW_DEGRADATION 10 |
55 |
|
|
56 |
/* Keyframe settings */ |
/* Keyframe settings */ |
57 |
#define DEFAULT_KFREDUCTION 20 |
#define DEFAULT_KFREDUCTION 20 |
58 |
#define DEFAULT_MIN_KEY_INTERVAL 1 |
#define DEFAULT_KFTHRESHOLD 1 |
59 |
|
|
60 |
|
/***************************************************************************** |
61 |
|
* Some default constants (can be tuned) |
62 |
|
****************************************************************************/ |
63 |
|
|
64 |
|
/* Specify the invariant part of the headers bits (header+MV) |
65 |
|
* as hlength/cst */ |
66 |
|
#define INVARIANT_HEADER_PART_IVOP 1 /* factor 1.0f */ |
67 |
|
#define INVARIANT_HEADER_PART_PVOP 2 /* factor 0.5f */ |
68 |
|
#define INVARIANT_HEADER_PART_BVOP 8 /* factor 0.125f */ |
69 |
|
|
70 |
/***************************************************************************** |
/***************************************************************************** |
71 |
* Structures |
* Structures |
75 |
typedef struct { |
typedef struct { |
76 |
int type; /* first pass type */ |
int type; /* first pass type */ |
77 |
int quant; /* first pass quant */ |
int quant; /* first pass quant */ |
|
int quant2; /* Second pass quant */ |
|
78 |
int blks[3]; /* k,m,y blks */ |
int blks[3]; /* k,m,y blks */ |
79 |
int length; /* first pass length */ |
int length; /* first pass length */ |
80 |
|
int invariant; /* what we assume as being invariant between the two passes, it's a sub part of header + MV bits */ |
81 |
int scaled_length; /* scaled length */ |
int scaled_length; /* scaled length */ |
82 |
int desired_length; /* desired length; calculated during encoding */ |
int desired_length; /* desired length; calculated during encoding */ |
83 |
int error; |
int error; |
109 |
|
|
110 |
/* Total length of each frame types (1st pass) */ |
/* Total length of each frame types (1st pass) */ |
111 |
uint64_t tot_length[3]; |
uint64_t tot_length[3]; |
112 |
|
uint64_t tot_invariant[3]; |
113 |
|
|
114 |
/* Average length of each frame types (used first for 1st pass data and |
/* Average length of each frame types (used first for 1st pass data and |
115 |
* then for scaled averages */ |
* then for scaled averages */ |
130 |
|
|
131 |
/*---------------------------------- |
/*---------------------------------- |
132 |
* Zones statistical data |
* Zones statistical data |
|
* |
|
|
* ToDo: Fix zones, current |
|
|
* implementation is buggy |
|
133 |
*--------------------------------*/ |
*--------------------------------*/ |
134 |
|
|
|
/* Average weight of the zones */ |
|
|
double avg_weight; |
|
|
|
|
135 |
/* Total length used by XVID_ZONE_QUANT zones */ |
/* Total length used by XVID_ZONE_QUANT zones */ |
136 |
int64_t tot_quant; |
uint64_t tot_quant; |
137 |
|
uint64_t tot_quant_invariant; |
138 |
|
|
139 |
|
/* Holds the total amount of frame bytes, zone weighted (only scalable |
140 |
|
* part of frame bytes) */ |
141 |
|
uint64_t tot_weighted; |
142 |
|
|
143 |
/*---------------------------------- |
/*---------------------------------- |
144 |
* Advanced settings helper ratios |
* Advanced settings helper ratios |
234 |
{ |
{ |
235 |
switch(opt) { |
switch(opt) { |
236 |
case XVID_PLG_INFO : |
case XVID_PLG_INFO : |
237 |
|
case XVID_PLG_FRAME : |
238 |
return 0; |
return 0; |
239 |
|
|
240 |
case XVID_PLG_CREATE : |
case XVID_PLG_CREATE : |
296 |
|
|
297 |
/* Keyframe settings */ |
/* Keyframe settings */ |
298 |
_INIT(rc->param.kfreduction, DEFAULT_KFREDUCTION); |
_INIT(rc->param.kfreduction, DEFAULT_KFREDUCTION); |
299 |
_INIT(rc->param.min_key_interval, DEFAULT_MIN_KEY_INTERVAL); |
_INIT(rc->param.kfthreshold, DEFAULT_KFTHRESHOLD); |
300 |
#undef _INIT |
#undef _INIT |
301 |
|
|
302 |
/* Initialize some stuff to zero */ |
/* Initialize some stuff to zero */ |
347 |
/* Compute the target filesize */ |
/* Compute the target filesize */ |
348 |
if (rc->param.bitrate<0) { |
if (rc->param.bitrate<0) { |
349 |
/* if negative, bitrate equals the target (in kbytes) */ |
/* if negative, bitrate equals the target (in kbytes) */ |
350 |
rc->target = (-rc->param.bitrate) * 1024; |
rc->target = ((uint64_t)(-rc->param.bitrate)) * 1024; |
351 |
} else if (rc->num_frames < create->fbase/create->fincr) { |
} else if (rc->num_frames < create->fbase/create->fincr) { |
352 |
/* Source sequence is less than 1s long, we do as if it was 1s long */ |
/* Source sequence is less than 1s long, we do as if it was 1s long */ |
353 |
rc->target = rc->param.bitrate / 8; |
rc->target = rc->param.bitrate / 8; |
373 |
if(rc->param.container_frame_overhead) |
if(rc->param.container_frame_overhead) |
374 |
DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- New target filesize after container compensation: %lld\n", rc->target); |
DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- New target filesize after container compensation: %lld\n", rc->target); |
375 |
|
|
376 |
|
/* When bitrate is not given it means it has been scaled by an external |
377 |
|
* application */ |
378 |
|
if (rc->param.bitrate) { |
379 |
|
/* Apply zone settings |
380 |
|
* - set rc->tot_quant which represents the total num of bytes spent in |
381 |
|
* fixed quant zones |
382 |
|
* - set rc->tot_weighted which represents the total amount of bytes |
383 |
|
* spent in normal or weighted zones in first pass (normal zones can |
384 |
|
* be considered weight=1) |
385 |
|
* - set rc->tot_quant_invariant which represents the total num of bytes |
386 |
|
* spent in fixed quant zones for headers */ |
387 |
|
zone_process(rc, create); |
388 |
|
} else { |
389 |
|
/* External scaling -- zones are ignored */ |
390 |
|
for (i=0;i<rc->num_frames;i++) { |
391 |
|
rc->stats[i].zone_mode = XVID_ZONE_WEIGHT; |
392 |
|
rc->stats[i].weight = 1.0; |
393 |
|
} |
394 |
|
rc->tot_quant = 0; |
395 |
|
} |
396 |
|
|
397 |
/* Gathers some information about first pass stats: |
/* Gathers some information about first pass stats: |
398 |
* - finds the minimum frame length for each frame type during 1st pass. |
* - finds the minimum frame length for each frame type during 1st pass. |
399 |
* rc->min_size[] |
* rc->min_size[] |
402 |
* - count how many times each frame type has been used. |
* - count how many times each frame type has been used. |
403 |
* rc->count[] |
* rc->count[] |
404 |
* - total bytes used per frame type |
* - total bytes used per frame type |
405 |
* rc->total[] |
* rc->tot_length[] |
406 |
|
* - total bytes considered invariant between the 2 passes |
407 |
* - store keyframe location |
* - store keyframe location |
408 |
* rc->keyframe_locations[] |
* rc->keyframe_locations[] |
409 |
*/ |
*/ |
410 |
first_pass_stats_prepare_data(rc); |
first_pass_stats_prepare_data(rc); |
411 |
|
|
412 |
/* When bitrate is not given it means it has been scaled by an external |
/* If we have a user bitrate, it means it's an internal curve scaling */ |
|
* application */ |
|
413 |
if (rc->param.bitrate) { |
if (rc->param.bitrate) { |
|
/* Apply zone settings */ |
|
|
zone_process(rc, create); |
|
414 |
/* Perform internal curve scaling */ |
/* Perform internal curve scaling */ |
415 |
first_pass_scale_curve_internal(rc); |
first_pass_scale_curve_internal(rc); |
|
} else { |
|
|
/* External scaling -- zones are ignored */ |
|
|
for (i=0;i<rc->num_frames;i++) { |
|
|
rc->stats[i].zone_mode = XVID_ZONE_WEIGHT; |
|
|
rc->stats[i].weight = 1.0; |
|
|
} |
|
|
rc->avg_weight = 1.0; |
|
|
rc->tot_quant = 0; |
|
416 |
} |
} |
417 |
|
|
418 |
/* Apply advanced curve options, and compute some parameters in order to |
/* Apply advanced curve options, and compute some parameters in order to |
461 |
if (data->quant > 0) |
if (data->quant > 0) |
462 |
return(0); |
return(0); |
463 |
|
|
464 |
/* Second case: We are in a Quant zone */ |
/* Second case: insufficent stats data |
465 |
|
* We can't guess much what we should do, let core decide all alone */ |
466 |
|
if (data->frame_num >= rc->num_frames) { |
467 |
|
DPRINTF(XVID_DEBUG_RC,"[xvid rc] -- stats file too short (now processing frame %d)", |
468 |
|
data->frame_num); |
469 |
|
return(0); |
470 |
|
} |
471 |
|
|
472 |
|
/* Third case: We are in a Quant zone |
473 |
|
* Quant zones must just ensure we use the same settings as first pass |
474 |
|
* So set the quantizer and the type */ |
475 |
if (s->zone_mode == XVID_ZONE_QUANT) { |
if (s->zone_mode == XVID_ZONE_QUANT) { |
476 |
|
/* Quant stuff */ |
477 |
rc->fq_error += s->weight; |
rc->fq_error += s->weight; |
478 |
data->quant = (int)rc->fq_error; |
data->quant = (int)rc->fq_error; |
479 |
rc->fq_error -= data->quant; |
rc->fq_error -= data->quant; |
480 |
|
|
481 |
|
/* The type stuff */ |
482 |
|
data->type = s->type; |
483 |
|
|
484 |
|
/* The only required data for AFTER step is this one for the overflow |
485 |
|
* control */ |
486 |
s->desired_length = s->length; |
s->desired_length = s->length; |
487 |
|
|
488 |
return(0); |
return(0); |
489 |
} |
} |
490 |
|
|
|
/* Third case: insufficent stats data */ |
|
|
if (data->frame_num >= rc->num_frames) |
|
|
return(0); |
|
491 |
|
|
492 |
/*************************************************************************/ |
/*************************************************************************/ |
493 |
/*************************************************************************/ |
/*************************************************************************/ |
505 |
|
|
506 |
/* IFrame user settings*/ |
/* IFrame user settings*/ |
507 |
if (s->type == XVID_TYPE_IVOP) { |
if (s->type == XVID_TYPE_IVOP) { |
|
|
|
508 |
/* Keyframe boosting -- All keyframes benefit from it */ |
/* Keyframe boosting -- All keyframes benefit from it */ |
509 |
dbytes += dbytes*rc->param.keyframe_boost / 100; |
dbytes += dbytes*rc->param.keyframe_boost / 100; |
510 |
|
|
511 |
/* Applies keyframe penalties, but not the first frame */ |
#if 0 /* ToDo: decide how to apply kfthresholding */ |
512 |
if (rc->KF_idx) { |
#endif |
|
int penalty_distance; |
|
|
|
|
|
/* Minimum keyframe distance penalties */ |
|
|
penalty_distance = rc->param.min_key_interval; |
|
|
penalty_distance -= rc->keyframe_locations[rc->KF_idx]; |
|
|
penalty_distance += rc->keyframe_locations[rc->KF_idx-1]; |
|
|
|
|
|
/* Ah ah ! guilty keyframe, you're under arrest ! */ |
|
|
if (penalty_distance > 0) |
|
|
dbytes -= dbytes*penalty_distance*rc->param.kfreduction/100; |
|
|
} |
|
513 |
} else { |
} else { |
514 |
|
|
515 |
/* P/S/B frames must reserve some bits for iframe boosting */ |
/* P/S/B frames must reserve some bits for iframe boosting */ |
551 |
*-----------------------------------------------------------------------*/ |
*-----------------------------------------------------------------------*/ |
552 |
|
|
553 |
/* Compute the overflow we should compensate */ |
/* Compute the overflow we should compensate */ |
554 |
if (s->type != XVID_TYPE_IVOP) { |
if (s->type != XVID_TYPE_IVOP || rc->overflow > 0) { |
555 |
double frametype_factor; |
double frametype_factor; |
556 |
double framesize_factor; |
double framesize_factor; |
557 |
|
|
582 |
/* Apply the overflow strength imposed by the user */ |
/* Apply the overflow strength imposed by the user */ |
583 |
overflow *= (rc->param.overflow_control_strength/100.0f); |
overflow *= (rc->param.overflow_control_strength/100.0f); |
584 |
} else { |
} else { |
585 |
/* no overflow applied in IFrames because: |
/* no negative overflow applied in IFrames because: |
586 |
* - their role is important as they're references for P/BFrames. |
* - their role is important as they're references for P/BFrames. |
587 |
* - there aren't much in typical sequences, so if an IFrame overflows too |
* - there aren't much in typical sequences, so if an IFrame overflows too |
588 |
* much, this overflow may impact the next IFrame too much and generate |
* much, this overflow may impact the next IFrame too much and generate |
614 |
* pass nor smaller than the allowed minimum. |
* pass nor smaller than the allowed minimum. |
615 |
*-----------------------------------------------------------------------*/ |
*-----------------------------------------------------------------------*/ |
616 |
|
|
617 |
|
#ifdef PASS_SMALLER |
618 |
if (dbytes > s->length) { |
if (dbytes > s->length) { |
619 |
dbytes = s->length; |
dbytes = s->length; |
620 |
} else if (dbytes < rc->min_length[s->type-1]) { |
} else |
621 |
|
if (dbytes < rc->min_length[s->type-1]) { |
622 |
dbytes = rc->min_length[s->type-1]; |
dbytes = rc->min_length[s->type-1]; |
623 |
} else if (dbytes > rc->max_length) { |
} else if (dbytes > rc->max_length) { |
624 |
/* ToDo: this condition is always wrong as max_length == maximum frame |
/* ToDo: this condition is always wrong as max_length == maximum frame |
629 |
DPRINTF(XVID_DEBUG_RC,"[xvid rc] -- frame:%d Capped to maximum frame size\n", |
DPRINTF(XVID_DEBUG_RC,"[xvid rc] -- frame:%d Capped to maximum frame size\n", |
630 |
data->frame_num); |
data->frame_num); |
631 |
} |
} |
632 |
|
#endif |
633 |
/*------------------------------------------------------------------------ |
/*------------------------------------------------------------------------ |
634 |
* Desired frame length <-> quantizer mapping |
* Desired frame length <-> quantizer mapping |
635 |
*-----------------------------------------------------------------------*/ |
*-----------------------------------------------------------------------*/ |
636 |
|
|
637 |
/* For bframes we must retrieve the original quant used (sent to xvidcore) |
#ifdef BQUANT_PRESCALE |
638 |
* as core applies the bquant formula before writing the stat log entry */ |
/* For bframes we prescale the quantizer to avoid too high quant scaling */ |
639 |
if(s->type == XVID_TYPE_BVOP) { |
if(s->type == XVID_TYPE_BVOP) { |
640 |
|
|
641 |
twopass_stat_t *b_ref = s; |
twopass_stat_t *b_ref = s; |
645 |
b_ref--; |
b_ref--; |
646 |
|
|
647 |
/* Compute the original quant */ |
/* Compute the original quant */ |
648 |
s->quant = 100*s->quant - data->bquant_offset; |
s->quant = 2*(100*s->quant - data->bquant_offset); |
649 |
s->quant += data->bquant_ratio - 1; /* to avoid rouding issues */ |
s->quant += data->bquant_ratio - 1; /* to avoid rounding issues */ |
650 |
s->quant = s->quant/data->bquant_ratio - b_ref->quant; |
s->quant = s->quant/data->bquant_ratio - b_ref->quant; |
651 |
} |
} |
652 |
|
#endif |
653 |
|
|
654 |
/* Don't laugh at this very 'simple' quant<->filesize relationship, it |
/* Don't laugh at this very 'simple' quant<->size relationship, it |
655 |
* proves to be acurate enough for our algorithm */ |
* proves to be acurate enough for our algorithm */ |
656 |
scaled_quant = (double)s->quant*(double)s->length/(double)dbytes; |
scaled_quant = (double)s->quant*(double)s->length/(double)dbytes; |
657 |
|
|
739 |
/* Don't forget to force 1st pass frame type ;-) */ |
/* Don't forget to force 1st pass frame type ;-) */ |
740 |
data->type = s->type; |
data->type = s->type; |
741 |
|
|
|
/* Store the quantizer into the statistics -- Used to compensate the double |
|
|
* formula symptom */ |
|
|
s->quant2 = data->quant; |
|
|
|
|
742 |
return 0; |
return 0; |
743 |
} |
} |
744 |
|
|
800 |
rc->KFoverflow -= rc->KFoverflow_partial; |
rc->KFoverflow -= rc->KFoverflow_partial; |
801 |
} |
} |
802 |
|
|
803 |
rc->overflow += s->error = s->desired_length - data->length; |
rc->overflow += (s->error = s->desired_length - data->length); |
804 |
rc->real_total += data->length; |
rc->real_total += data->length; |
805 |
|
|
806 |
DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- frame:%d type:%c quant:%d stats:%d scaled:%d desired:%d actual:%d error:%d overflow:%.2f\n", |
DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- frame:%d type:%c quant:%d stats:%d scaled:%d desired:%d actual:%d error:%d overflow:%.2f\n", |
849 |
|
|
850 |
char *ptr; |
char *ptr; |
851 |
char type; |
char type; |
852 |
int fields, nouse; |
int fields; |
853 |
|
|
854 |
lines++; |
lines++; |
855 |
|
|
856 |
/* We skip spaces */ |
/* We skip spaces */ |
857 |
ptr = skipspaces(line); |
ptr = skipspaces(line); |
858 |
|
|
859 |
/* Skip coment lines */ |
/* Skip coment lines or empty lines */ |
860 |
if(iscomment(ptr)) { |
if(iscomment(ptr) || *ptr == '\0') { |
861 |
free(line); |
free(line); |
862 |
continue; |
continue; |
863 |
} |
} |
864 |
|
|
865 |
/* Read the stat line from buffer */ |
/* Read the stat line from buffer */ |
866 |
fields = sscanf(ptr, |
fields = sscanf(ptr, "%c", &type); |
|
"%c %d %d %d %d %d", |
|
|
&type, &nouse, &nouse, &nouse, &nouse, &nouse); |
|
867 |
|
|
868 |
/* Valid stats files have at least 6 fields */ |
/* Valid stats files have at least 7 fields */ |
869 |
if (fields == 6) { |
if (fields == 1) { |
870 |
switch(type) { |
switch(type) { |
871 |
case 'i': |
case 'i': |
872 |
case 'I': |
case 'I': |
887 |
} else { |
} else { |
888 |
DPRINTF(XVID_DEBUG_RC, |
DPRINTF(XVID_DEBUG_RC, |
889 |
"[xvid rc] -- WARNING: L%d misses some stat fields (%d).\n", |
"[xvid rc] -- WARNING: L%d misses some stat fields (%d).\n", |
890 |
lines, 6-fields); |
lines, 7-fields); |
891 |
} |
} |
892 |
|
|
893 |
/* Free the line buffer */ |
/* Free the line buffer */ |
925 |
/* We skip spaces */ |
/* We skip spaces */ |
926 |
ptr = skipspaces(line); |
ptr = skipspaces(line); |
927 |
|
|
928 |
/* Skip comment lines */ |
/* Skip comment lines or empty lines */ |
929 |
if(iscomment(ptr)) { |
if(iscomment(ptr) || *ptr == '\0') { |
930 |
free(line); |
free(line); |
931 |
continue; |
continue; |
932 |
} |
} |
936 |
|
|
937 |
/* Convert the fields */ |
/* Convert the fields */ |
938 |
fields = sscanf(ptr, |
fields = sscanf(ptr, |
939 |
"%c %d %d %d %d %d %d\n", |
"%c %d %d %d %d %d %d %d\n", |
940 |
&type, |
&type, |
941 |
&s->quant, |
&s->quant, |
942 |
&s->blks[0], &s->blks[1], &s->blks[2], |
&s->blks[0], &s->blks[1], &s->blks[2], |
943 |
&s->length, |
&s->length, &s->invariant /* not really yet */, |
944 |
&s->scaled_length); |
&s->scaled_length); |
945 |
|
|
946 |
/* Free line buffer, we don't need it anymore */ |
/* Free line buffer, we don't need it anymore */ |
948 |
|
|
949 |
/* Fail silently, this has probably been warned in |
/* Fail silently, this has probably been warned in |
950 |
* statsfile_count_frames */ |
* statsfile_count_frames */ |
951 |
if(fields != 6 && fields != 7) |
if(fields != 7 && fields != 8) |
952 |
continue; |
continue; |
953 |
|
|
954 |
/* Convert frame type */ |
/* Convert frame type and compute the invariant length part */ |
955 |
switch(type) { |
switch(type) { |
956 |
case 'i': |
case 'i': |
957 |
case 'I': |
case 'I': |
958 |
s->type = XVID_TYPE_IVOP; |
s->type = XVID_TYPE_IVOP; |
959 |
|
s->invariant /= INVARIANT_HEADER_PART_IVOP; |
960 |
break; |
break; |
961 |
case 'p': |
case 'p': |
962 |
case 'P': |
case 'P': |
963 |
case 's': |
case 's': |
964 |
case 'S': |
case 'S': |
965 |
s->type = XVID_TYPE_PVOP; |
s->type = XVID_TYPE_PVOP; |
966 |
|
s->invariant /= INVARIANT_HEADER_PART_PVOP; |
967 |
break; |
break; |
968 |
case 'b': |
case 'b': |
969 |
case 'B': |
case 'B': |
970 |
s->type = XVID_TYPE_BVOP; |
s->type = XVID_TYPE_BVOP; |
971 |
|
s->invariant /= INVARIANT_HEADER_PART_BVOP; |
972 |
break; |
break; |
973 |
default: |
default: |
974 |
/* Same as before, fail silently */ |
/* Same as before, fail silently */ |
987 |
|
|
988 |
/* pre-process the statistics data |
/* pre-process the statistics data |
989 |
* - for each type, count, tot_length, min_length, max_length |
* - for each type, count, tot_length, min_length, max_length |
990 |
* - set keyframes_locations */ |
* - set keyframes_locations, tot_prescaled */ |
991 |
static void |
static void |
992 |
first_pass_stats_prepare_data(rc_2pass2_t * rc) |
first_pass_stats_prepare_data(rc_2pass2_t * rc) |
993 |
{ |
{ |
999 |
for (i=0; i<3; i++) { |
for (i=0; i<3; i++) { |
1000 |
rc->count[i]=0; |
rc->count[i]=0; |
1001 |
rc->tot_length[i] = 0; |
rc->tot_length[i] = 0; |
1002 |
|
rc->tot_invariant[i] = 0; |
1003 |
rc->min_length[i] = INT_MAX; |
rc->min_length[i] = INT_MAX; |
1004 |
} |
} |
1005 |
|
|
1006 |
rc->max_length = INT_MIN; |
rc->max_length = INT_MIN; |
1007 |
|
rc->tot_weighted = 0; |
1008 |
|
|
1009 |
/* Loop through all frames and find/compute all the stuff this function |
/* Loop through all frames and find/compute all the stuff this function |
1010 |
* is supposed to do */ |
* is supposed to do */ |
1013 |
|
|
1014 |
rc->count[s->type-1]++; |
rc->count[s->type-1]++; |
1015 |
rc->tot_length[s->type-1] += s->length; |
rc->tot_length[s->type-1] += s->length; |
1016 |
|
rc->tot_invariant[s->type-1] += s->invariant; |
1017 |
|
if (s->zone_mode != XVID_ZONE_QUANT) |
1018 |
|
rc->tot_weighted += (int)(s->weight*(s->length - s->invariant)); |
1019 |
|
|
1020 |
if (s->length < rc->min_length[s->type-1]) { |
if (s->length < rc->min_length[s->type-1]) { |
1021 |
rc->min_length[s->type-1] = s->length; |
rc->min_length[s->type-1] = s->length; |
1050 |
int i,j; |
int i,j; |
1051 |
int n = 0; |
int n = 0; |
1052 |
|
|
|
rc->avg_weight = 0.0; |
|
1053 |
rc->tot_quant = 0; |
rc->tot_quant = 0; |
1054 |
|
rc->tot_quant_invariant = 0; |
1055 |
|
|
1056 |
if (create->num_zones == 0) { |
if (create->num_zones == 0) { |
1057 |
for (j = 0; j < rc->num_frames; j++) { |
for (j = 0; j < rc->num_frames; j++) { |
1058 |
rc->stats[j].zone_mode = XVID_ZONE_WEIGHT; |
rc->stats[j].zone_mode = XVID_ZONE_WEIGHT; |
1059 |
rc->stats[j].weight = 1.0; |
rc->stats[j].weight = 1.0; |
1060 |
} |
} |
|
rc->avg_weight += rc->num_frames * 1.0; |
|
1061 |
n += rc->num_frames; |
n += rc->num_frames; |
1062 |
} |
} |
1063 |
|
|
1066 |
|
|
1067 |
int next = (i+1<create->num_zones) ? create->zones[i+1].frame : rc->num_frames; |
int next = (i+1<create->num_zones) ? create->zones[i+1].frame : rc->num_frames; |
1068 |
|
|
1069 |
|
/* Zero weight make no sense */ |
1070 |
|
if (create->zones[i].increment == 0) create->zones[i].increment = 1; |
1071 |
|
/* And obviously an undetermined infinite makes even less sense */ |
1072 |
|
if (create->zones[i].base == 0) create->zones[i].base = 1; |
1073 |
|
|
1074 |
if (i==0 && create->zones[i].frame > 0) { |
if (i==0 && create->zones[i].frame > 0) { |
1075 |
for (j = 0; j < create->zones[i].frame && j < rc->num_frames; j++) { |
for (j = 0; j < create->zones[i].frame && j < rc->num_frames; j++) { |
1076 |
rc->stats[j].zone_mode = XVID_ZONE_WEIGHT; |
rc->stats[j].zone_mode = XVID_ZONE_WEIGHT; |
1077 |
rc->stats[j].weight = 1.0; |
rc->stats[j].weight = 1.0; |
1078 |
} |
} |
|
rc->avg_weight += create->zones[i].frame * 1.0; |
|
1079 |
n += create->zones[i].frame; |
n += create->zones[i].frame; |
1080 |
} |
} |
1081 |
|
|
1085 |
rc->stats[j].weight = (double)create->zones[i].increment / (double)create->zones[i].base; |
rc->stats[j].weight = (double)create->zones[i].increment / (double)create->zones[i].base; |
1086 |
} |
} |
1087 |
next -= create->zones[i].frame; |
next -= create->zones[i].frame; |
|
rc->avg_weight += (double)(next * create->zones[i].increment) / (double)create->zones[i].base; |
|
1088 |
n += next; |
n += next; |
1089 |
}else{ /* XVID_ZONE_QUANT */ |
}else{ /* XVID_ZONE_QUANT */ |
1090 |
for (j = create->zones[i].frame; j < next && j < rc->num_frames; j++ ) { |
for (j = create->zones[i].frame; j < next && j < rc->num_frames; j++ ) { |
1091 |
rc->stats[j].zone_mode = XVID_ZONE_QUANT; |
rc->stats[j].zone_mode = XVID_ZONE_QUANT; |
1092 |
rc->stats[j].weight = (double)create->zones[i].increment / (double)create->zones[i].base; |
rc->stats[j].weight = (double)create->zones[i].increment / (double)create->zones[i].base; |
1093 |
rc->tot_quant += rc->stats[j].length; |
rc->tot_quant += rc->stats[j].length; |
1094 |
|
rc->tot_quant_invariant += rc->stats[j].invariant; |
1095 |
} |
} |
1096 |
} |
} |
1097 |
} |
} |
|
rc->avg_weight = n>0 ? rc->avg_weight/n : 1.0; |
|
|
|
|
|
DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- center_weight:%f (for %d frames) fixed_bytes:%d\n", rc->avg_weight, n, rc->tot_quant); |
|
1098 |
} |
} |
1099 |
|
|
1100 |
|
|
1103 |
first_pass_scale_curve_internal(rc_2pass2_t *rc) |
first_pass_scale_curve_internal(rc_2pass2_t *rc) |
1104 |
{ |
{ |
1105 |
int64_t target; |
int64_t target; |
1106 |
int64_t pass1_length; |
int64_t total_invariant; |
1107 |
double scaler; |
double scaler; |
1108 |
int i, num_MBs; |
int i, num_MBs; |
1109 |
|
|
1110 |
/* We remove the bytes used by the fixed quantizer zones |
/* We only scale texture data ! */ |
1111 |
* ToDo: this approach is flawed, the same amount of bytes is removed from |
total_invariant = rc->tot_invariant[XVID_TYPE_IVOP-1]; |
1112 |
* target and first pass data, this has no sense, zone_process should |
total_invariant += rc->tot_invariant[XVID_TYPE_PVOP-1]; |
1113 |
* give us two results one for unscaled data (1pass) and the other |
total_invariant += rc->tot_invariant[XVID_TYPE_BVOP-1]; |
1114 |
* one for scaled data and we should then write: |
/* don't forget to substract header bytes used in quant zones, otherwise we |
1115 |
* target = rc->target - rc->tot_quant_scaled; |
* counting them twice */ |
1116 |
* pass1_length = rc->i+p+b - rc->tot_quant_firstpass */ |
total_invariant -= rc->tot_quant_invariant; |
1117 |
target = rc->target - rc->tot_quant; |
|
1118 |
|
/* We remove the bytes used by the fixed quantizer zones during first pass |
1119 |
/* Do the same for the first pass data */ |
* with the same quants, so we know very precisely how much that |
1120 |
pass1_length = rc->tot_length[XVID_TYPE_IVOP-1]; |
* represents */ |
1121 |
pass1_length += rc->tot_length[XVID_TYPE_PVOP-1]; |
target = rc->target; |
1122 |
pass1_length += rc->tot_length[XVID_TYPE_BVOP-1]; |
target -= rc->tot_quant; |
|
pass1_length -= rc->tot_quant; |
|
1123 |
|
|
1124 |
/* Let's compute a linear scaler in order to perform curve scaling */ |
/* Let's compute a linear scaler in order to perform curve scaling */ |
1125 |
scaler = (double)target / (double)pass1_length; |
scaler = (double)(target - total_invariant) / (double)(rc->tot_weighted); |
|
|
|
|
if (target <= 0 || pass1_length <= 0 || target >= pass1_length) { |
|
|
DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- WARNING: Undersize detected before correction\n"); |
|
|
scaler = 1.0; |
|
|
} |
|
1126 |
|
|
1127 |
/* Compute min frame lengths (for each frame type) according to the number |
/* Compute min frame lengths (for each frame type) according to the number |
1128 |
* of MBs. We sum all block type counters of frame 0, this gives us the |
* of MBs. We sum all block type counters of frame 0, this gives us the |
1164 |
continue; |
continue; |
1165 |
} |
} |
1166 |
|
|
1167 |
/* Compute the scaled length */ |
/* Compute the scaled length -- only non invariant data length is scaled */ |
1168 |
len = (int)((double)s->length * scaler * s->weight / rc->avg_weight); |
len = s->invariant + (int)((double)(s->length-s->invariant) * scaler * s->weight); |
1169 |
|
|
1170 |
/* Compare with the computed minimum */ |
/* Compare with the computed minimum */ |
1171 |
if (len < rc->min_length[s->type-1]) { |
if (len < rc->min_length[s->type-1]) { |
1177 |
* total counters, as we prepare a second pass for 'regular' |
* total counters, as we prepare a second pass for 'regular' |
1178 |
* frames */ |
* frames */ |
1179 |
target -= s->scaled_length; |
target -= s->scaled_length; |
|
pass1_length -= s->length; |
|
1180 |
} else { |
} else { |
1181 |
/* Do nothing for now, we'll scale this later */ |
/* Do nothing for now, we'll scale this later */ |
1182 |
s->scaled_length = 0; |
s->scaled_length = 0; |
1187 |
* total counters. Now, it's possible to scale the 'regular' frames. */ |
* total counters. Now, it's possible to scale the 'regular' frames. */ |
1188 |
|
|
1189 |
/* Scaling factor for 'regular' frames */ |
/* Scaling factor for 'regular' frames */ |
1190 |
scaler = (double)target / (double)pass1_length; |
scaler = (double)(target - total_invariant) / (double)(rc->tot_weighted); |
|
|
|
|
/* Detect undersizing */ |
|
|
if (target <= 0 || pass1_length <= 0 || target >= pass1_length) { |
|
|
DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- WARNING: Undersize detected after correction\n"); |
|
|
scaler = 1.0; |
|
|
} |
|
1191 |
|
|
1192 |
/* Do another pass with the new scaler */ |
/* Do another pass with the new scaler */ |
1193 |
for (i=0; i<rc->num_frames; i++) { |
for (i=0; i<rc->num_frames; i++) { |
1195 |
|
|
1196 |
/* Ignore frame with forced frame sizes */ |
/* Ignore frame with forced frame sizes */ |
1197 |
if (s->scaled_length == 0) |
if (s->scaled_length == 0) |
1198 |
s->scaled_length = (int)((double)s->length * scaler * s->weight / rc->avg_weight); |
s->scaled_length = s->invariant + (int)((double)(s->length-s->invariant) * scaler * s->weight); |
1199 |
} |
} |
1200 |
|
|
1201 |
/* Job done */ |
/* Job done */ |
1210 |
scaled_curve_apply_advanced_parameters(rc_2pass2_t * rc) |
scaled_curve_apply_advanced_parameters(rc_2pass2_t * rc) |
1211 |
{ |
{ |
1212 |
int i; |
int i; |
1213 |
uint64_t ivop_boost_total; |
int64_t ivop_boost_total; |
1214 |
|
|
1215 |
/* Reset the rate controller (per frame type) total byte counters */ |
/* Reset the rate controller (per frame type) total byte counters */ |
1216 |
for (i=0; i<3; i++) rc->tot_scaled_length[i] = 0; |
for (i=0; i<3; i++) rc->tot_scaled_length[i] = 0; |
1233 |
|
|
1234 |
/* Some more work is needed for I frames */ |
/* Some more work is needed for I frames */ |
1235 |
if (s->type == XVID_TYPE_IVOP) { |
if (s->type == XVID_TYPE_IVOP) { |
|
int penalty_distance; |
|
1236 |
int ivop_boost; |
int ivop_boost; |
1237 |
|
|
1238 |
/* Accumulate bytes needed for keyframe boosting */ |
/* Accumulate bytes needed for keyframe boosting */ |
1239 |
ivop_boost = s->scaled_length*rc->param.keyframe_boost/100; |
ivop_boost = s->scaled_length*rc->param.keyframe_boost/100; |
1240 |
|
|
1241 |
if (rc->KF_idx) { |
#if 0 /* ToDo: decide how to apply kfthresholding */ |
1242 |
/* Minimum keyframe distance penalties */ |
#endif |
|
penalty_distance = rc->param.min_key_interval; |
|
|
penalty_distance -= rc->keyframe_locations[rc->KF_idx]; |
|
|
penalty_distance += rc->keyframe_locations[rc->KF_idx-1]; |
|
|
|
|
|
/* Ah ah ! guilty keyframe, you're under arrest ! */ |
|
|
if (penalty_distance > 0) |
|
|
ivop_boost -= (s->scaled_length + ivop_boost)*penalty_distance*rc->param.kfreduction/100; |
|
|
} |
|
|
|
|
1243 |
/* If the frame size drops under the minimum length, then cap ivop_boost */ |
/* If the frame size drops under the minimum length, then cap ivop_boost */ |
1244 |
if (ivop_boost + s->scaled_length < rc->min_length[XVID_TYPE_IVOP-1]) |
if (ivop_boost + s->scaled_length < rc->min_length[XVID_TYPE_IVOP-1]) |
1245 |
ivop_boost = rc->min_length[XVID_TYPE_IVOP-1] - s->scaled_length; |
ivop_boost = rc->min_length[XVID_TYPE_IVOP-1] - s->scaled_length; |
1269 |
|
|
1270 |
/* Compute the ratio described above |
/* Compute the ratio described above |
1271 |
* taxed_total = sum(0, n, tax*scaled_length) |
* taxed_total = sum(0, n, tax*scaled_length) |
1272 |
* <=> taxed_total = tax.sum(0, n, tax*scaled_length) |
* <=> taxed_total = tax.sum(0, n, scaled_length) |
1273 |
* <=> tax = taxed_total / original_total */ |
* <=> tax = taxed_total / original_total */ |
1274 |
rc->pb_iboost_tax_ratio = |
rc->pb_iboost_tax_ratio = |
1275 |
(rc->pb_iboost_tax_ratio - ivop_boost_total) / |
(rc->pb_iboost_tax_ratio - ivop_boost_total) / |
1286 |
} else { |
} else { |
1287 |
rc->avg_length[i] = rc->tot_scaled_length[i]; |
rc->avg_length[i] = rc->tot_scaled_length[i]; |
1288 |
|
|
1289 |
if (i == XVID_TYPE_IVOP) { |
if (i == (XVID_TYPE_IVOP-1)) { |
1290 |
/* I Frames total has to be added the boost total */ |
/* I Frames total has to be added the boost total */ |
1291 |
rc->avg_length[i] += ivop_boost_total; |
rc->avg_length[i] += ivop_boost_total; |
1292 |
} else { |
} else { |