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.1.2.8 2003-05-22 16:36:07 edgomez Exp $ |
* $Id: plugin_2pass2.c,v 1.1.2.9 2003-05-22 22:17:44 edgomez Exp $ |
29 |
* |
* |
30 |
*****************************************************************************/ |
*****************************************************************************/ |
31 |
|
|
32 |
#include <stdio.h> |
#include <stdio.h> |
33 |
#include <math.h> |
#include <math.h> |
34 |
|
#include <limits.h> |
35 |
|
|
36 |
#define RAD2DEG 57.295779513082320876798154814105 |
#define RAD2DEG 57.295779513082320876798154814105 |
37 |
#define DEG2RAD 0.017453292519943295769236907684886 |
#define DEG2RAD 0.017453292519943295769236907684886 |
195 |
static void print_stats(rc_2pass2_t * rc) |
static void print_stats(rc_2pass2_t * rc) |
196 |
{ |
{ |
197 |
int i; |
int i; |
198 |
|
DPRINTF(XVID_DEBUG_RC, "type quant length scaled_length\n"); |
199 |
for (i = 0; i < rc->num_frames; i++) { |
for (i = 0; i < rc->num_frames; i++) { |
200 |
stat_t * s = &rc->stats[i]; |
stat_t * s = &rc->stats[i]; |
201 |
DPRINTF(XVID_DEBUG_RC, "%i %i %i %i\n", s->type, s->quant, s->length, s->scaled_length); |
DPRINTF(XVID_DEBUG_RC, "%d %d %d %d\n", s->type, s->quant, s->length, s->scaled_length); |
202 |
} |
} |
203 |
} |
} |
204 |
#endif |
#endif |
208 |
- set keyframes_locations |
- set keyframes_locations |
209 |
*/ |
*/ |
210 |
|
|
211 |
void pre_process0(rc_2pass2_t * rc) |
static void |
212 |
|
pre_process0(rc_2pass2_t * rc) |
213 |
{ |
{ |
214 |
int i,j; |
int i,j; |
215 |
|
|
217 |
rc->count[i]=0; |
rc->count[i]=0; |
218 |
rc->tot_length[i] = 0; |
rc->tot_length[i] = 0; |
219 |
rc->last_quant[i] = 0; |
rc->last_quant[i] = 0; |
220 |
|
rc->min_length[i] = INT_MAX; |
221 |
} |
} |
222 |
|
|
223 |
|
rc->max_length = INT_MIN; |
224 |
|
|
225 |
for (i=j=0; i<rc->num_frames; i++) { |
for (i=j=0; i<rc->num_frames; i++) { |
226 |
stat_t * s = &rc->stats[i]; |
stat_t * s = &rc->stats[i]; |
227 |
|
|
228 |
rc->count[s->type-1]++; |
rc->count[s->type-1]++; |
229 |
rc->tot_length[s->type-1] += s->length; |
rc->tot_length[s->type-1] += s->length; |
230 |
|
|
231 |
if (i == 0 || s->length < rc->min_length[s->type-1]) { |
if (s->length < rc->min_length[s->type-1]) { |
232 |
rc->min_length[s->type-1] = s->length; |
rc->min_length[s->type-1] = s->length; |
233 |
} |
} |
234 |
|
|
235 |
if (i == 0 || s->length > rc->max_length) { |
if (s->length > rc->max_length) { |
236 |
rc->max_length = s->length; |
rc->max_length = s->length; |
237 |
} |
} |
238 |
|
|
249 |
* frame to appear in the keyframe locations array. |
* frame to appear in the keyframe locations array. |
250 |
*/ |
*/ |
251 |
rc->keyframe_locations[j] = i; |
rc->keyframe_locations[j] = i; |
252 |
|
|
253 |
|
DPRINTF(XVID_DEBUG_RC, "Min 1st pass IFrame length: %d\n", rc->min_length[0]); |
254 |
|
DPRINTF(XVID_DEBUG_RC, "Min 1st pass PFrame length: %d\n", rc->min_length[1]); |
255 |
|
DPRINTF(XVID_DEBUG_RC, "Min 1st pass BFrame length: %d\n", rc->min_length[2]); |
256 |
} |
} |
257 |
|
|
258 |
|
|
259 |
/* calculate zone weight "center" */ |
/* calculate zone weight "center" */ |
260 |
|
|
261 |
static void zone_process(rc_2pass2_t *rc, const xvid_plg_create_t * create) |
static void |
262 |
|
zone_process(rc_2pass2_t *rc, const xvid_plg_create_t * create) |
263 |
{ |
{ |
264 |
int i,j; |
int i,j; |
265 |
int n = 0; |
int n = 0; |
315 |
|
|
316 |
/* scale the curve */ |
/* scale the curve */ |
317 |
|
|
318 |
static void internal_scale(rc_2pass2_t *rc) |
static void |
319 |
|
internal_scale(rc_2pass2_t *rc) |
320 |
{ |
{ |
321 |
int64_t target = rc->target - rc->tot_quant; |
int64_t target = rc->target - rc->tot_quant; |
322 |
int64_t pass1_length = rc->tot_length[0] + rc->tot_length[1] + rc->tot_length[2] - rc->tot_quant; |
int64_t pass1_length = rc->tot_length[0] + rc->tot_length[1] + rc->tot_length[2] - rc->tot_quant; |
325 |
int i; |
int i; |
326 |
|
|
327 |
|
|
328 |
/* perform an initial scale pass. |
/* |
329 |
if a frame size is scaled underneath our hardcoded minimums, then we force the |
* Perform an initial scale pass. |
330 |
frame size to the minimum, and deduct the original & scaled frmae length from the |
* if a frame size is scaled underneath our hardcoded minimums, then we |
331 |
original and target total lengths */ |
* force the frame size to the minimum, and deduct the original & scaled |
332 |
|
* frame length from the original and target total lengths |
333 |
|
*/ |
334 |
|
|
335 |
min_size[0] = ((rc->stats[0].blks[0]*22) + 240) / 8; |
min_size[0] = ((rc->stats[0].blks[0]*22) + 240) / 8; |
336 |
min_size[1] = (rc->stats[0].blks[0] + 88) / 8; |
min_size[1] = (rc->stats[0].blks[0] + 88) / 8; |
343 |
scaler = 1.0; |
scaler = 1.0; |
344 |
} |
} |
345 |
|
|
346 |
DPRINTF(XVID_DEBUG_RC, "target=%i, tot_length=%i, scaler=%f\n", (int)target, (int)pass1_length, scaler); |
DPRINTF(XVID_DEBUG_RC, |
347 |
|
"Before any correction: target=%i, tot_length=%i, scaler=%f\n", |
348 |
|
(int)target, (int)pass1_length, scaler); |
349 |
|
|
350 |
for (i=0; i<rc->num_frames; i++) { |
for (i=0; i<rc->num_frames; i++) { |
351 |
stat_t * s = &rc->stats[i]; |
stat_t * s = &rc->stats[i]; |
371 |
scaler = 1.0; |
scaler = 1.0; |
372 |
} |
} |
373 |
|
|
374 |
DPRINTF(XVID_DEBUG_RC, "target=%i, tot_length=%i, scaler=%f\n", (int)target, (int)pass1_length, scaler); |
DPRINTF(XVID_DEBUG_RC, |
375 |
|
"After correction: target=%i, tot_length=%i, scaler=%f\n", |
376 |
|
(int)target, (int)pass1_length, scaler); |
377 |
|
|
378 |
for (i=0; i<rc->num_frames; i++) { |
for (i=0; i<rc->num_frames; i++) { |
379 |
stat_t * s = &rc->stats[i]; |
stat_t * s = &rc->stats[i]; |
387 |
|
|
388 |
|
|
389 |
|
|
390 |
void pre_process1(rc_2pass2_t * rc) |
static void |
391 |
|
pre_process1(rc_2pass2_t * rc) |
392 |
{ |
{ |
393 |
int i; |
int i; |
394 |
double total1, total2; |
double total1, total2; |
694 |
if (rc->num_frames < create->fbase/create->fincr) { |
if (rc->num_frames < create->fbase/create->fincr) { |
695 |
rc->target = rc->param.bitrate / 8; /* one second */ |
rc->target = rc->param.bitrate / 8; /* one second */ |
696 |
}else{ |
}else{ |
697 |
rc->target = (rc->param.bitrate * rc->num_frames * create->fincr) / (create->fbase * 8); |
rc->target = |
698 |
|
((uint64_t)rc->param.bitrate * (uint64_t)rc->num_frames * (uint64_t)create->fincr) / \ |
699 |
|
((uint64_t)create->fbase * 8); |
700 |
} |
} |
701 |
|
|
702 |
DPRINTF(XVID_DEBUG_RC, "rc->target : %i\n", rc->target); |
DPRINTF(XVID_DEBUG_RC, "Number of frames: %d\n", rc->num_frames); |
703 |
|
DPRINTF(XVID_DEBUG_RC, "Frame rate: %d/%d\n", create->fbase, create->fincr); |
704 |
|
DPRINTF(XVID_DEBUG_RC, "Target bitrate: %ld\n", rc->param.bitrate); |
705 |
|
DPRINTF(XVID_DEBUG_RC, "Target filesize: %lld\n", rc->target); |
706 |
|
|
707 |
#if 0 |
#if 0 |
708 |
rc->target -= rc->num_frames*24; /* avi file header */ |
rc->target -= rc->num_frames*24; /* avi file header */ |
1005 |
if (desired > rc->max_length) { |
if (desired > rc->max_length) { |
1006 |
capped_to_max_framesize = 1; |
capped_to_max_framesize = 1; |
1007 |
desired = rc->max_length; |
desired = rc->max_length; |
1008 |
|
DPRINTF(XVID_DEBUG_RC,"[%i] Capped to maximum frame size\n", |
1009 |
|
data->frame_num); |
1010 |
} |
} |
1011 |
|
|
1012 |
/* Make sure to not scale below the minimum framesize */ |
/* Make sure to not scale below the minimum framesize */ |
1013 |
if (desired < rc->min_length[s->type-1]) |
if (desired < rc->min_length[s->type-1]) { |
1014 |
desired = rc->min_length[s->type-1]; |
desired = rc->min_length[s->type-1]; |
1015 |
|
DPRINTF(XVID_DEBUG_RC,"[%i] Capped to minimum frame size\n", |
1016 |
|
data->frame_num); |
1017 |
|
} |
1018 |
|
|
1019 |
/* |
/* |
1020 |
* Don't laugh at this very 'simple' quant<->filesize relationship, it |
* Don't laugh at this very 'simple' quant<->filesize relationship, it |
1076 |
|
|
1077 |
if (data->quant > rc->last_quant[s->type-1] + 2) { |
if (data->quant > rc->last_quant[s->type-1] + 2) { |
1078 |
data->quant = rc->last_quant[s->type-1] + 2; |
data->quant = rc->last_quant[s->type-1] + 2; |
1079 |
DPRINTF(XVID_DEBUG_RC, "p/b-frame quantizer prevented from rising too steeply\n"); |
DPRINTF(XVID_DEBUG_RC, |
1080 |
|
"[%i] p/b-frame quantizer prevented from rising too steeply\n", |
1081 |
|
data->frame_num); |
1082 |
} |
} |
1083 |
if (data->quant < rc->last_quant[s->type-1] - 2) { |
if (data->quant < rc->last_quant[s->type-1] - 2) { |
1084 |
data->quant = rc->last_quant[s->type-1] - 2; |
data->quant = rc->last_quant[s->type-1] - 2; |
1085 |
DPRINTF(XVID_DEBUG_RC, "p/b-frame quantizer prevented from falling too steeply\n"); |
DPRINTF(XVID_DEBUG_RC, |
1086 |
|
"[%i] p/b-frame quantizer prevented from falling too steeply\n", |
1087 |
|
data->frame_num); |
1088 |
} |
} |
1089 |
} |
} |
1090 |
|
|