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

Annotation of /xvidcore/src/plugins/plugin_2pass2.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1.2.13 - (view) (download)

1 : suxen_drol 1.1.2.1 /******************************************************************************
2 :     *
3 :     * XviD Bit Rate Controller Library
4 : edgomez 1.1.2.11 * - VBR 2 pass bitrate controller implementation -
5 : suxen_drol 1.1.2.1 *
6 : edgomez 1.1.2.7 * Copyright (C) 2002 Foxer <email?>
7 :     * 2002 Dirk Knop <dknop@gwdg.de>
8 :     * 2002-2003 Edouard Gomez <ed.gomez@free.fr>
9 :     * 2003 Pete Ross <pross@xvid.org>
10 : suxen_drol 1.1.2.1 *
11 : edgomez 1.1.2.7 * This curve treatment algorithm is the one originally implemented by Foxer
12 :     * and tuned by Dirk Knop for the XviD vfw frontend.
13 : suxen_drol 1.1.2.1 *
14 :     * This program is free software; you can redistribute it and/or modify
15 :     * it under the terms of the GNU General Public License as published by
16 :     * the Free Software Foundation; either version 2 of the License, or
17 :     * (at your option) any later version.
18 :     *
19 :     * This program is distributed in the hope that it will be useful,
20 :     * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 :     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 :     * GNU General Public License for more details.
23 :     *
24 :     * You should have received a copy of the GNU General Public License
25 :     * along with this program; if not, write to the Free Software
26 :     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 :     *
28 : edgomez 1.1.2.13 * $Id: plugin_2pass2.c,v 1.1.2.12 2003/05/24 22:03:50 edgomez Exp $
29 : suxen_drol 1.1.2.1 *
30 :     *****************************************************************************/
31 :    
32 :     #include <stdio.h>
33 :     #include <math.h>
34 : edgomez 1.1.2.9 #include <limits.h>
35 : suxen_drol 1.1.2.1
36 : edgomez 1.1.2.11 #include "../xvid.h"
37 :     #include "../image/image.h"
38 :    
39 :     /*****************************************************************************
40 :     * Some constants
41 :     ****************************************************************************/
42 :    
43 :     #define DEFAULT_KEYFRAME_BOOST 0
44 :     #define DEFAULT_PAYBACK_METHOD XVID_PAYBACK_PROP
45 :     #define DEFAULT_BITRATE_PAYBACK_DELAY 250
46 :     #define DEFAULT_CURVE_COMPRESSION_HIGH 0
47 :     #define DEFAULT_CURVE_COMPRESSION_LOW 0
48 :     #define DEFAULT_MAX_OVERFLOW_IMPROVEMENT 60
49 :     #define DEFAULT_MAX_OVERFLOW_DEGRADATION 60
50 :    
51 :     /* Alt curve settings */
52 :     #define DEFAULT_USE_ALT_CURVE 0
53 :     #define DEFAULT_ALT_CURVE_HIGH_DIST 500
54 :     #define DEFAULT_ALT_CURVE_LOW_DIST 90
55 :     #define DEFAULT_ALT_CURVE_USE_AUTO 1
56 :     #define DEFAULT_ALT_CURVE_AUTO_STR 30
57 :     #define DEFAULT_ALT_CURVE_TYPE XVID_CURVE_LINEAR
58 :     #define DEFAULT_ALT_CURVE_MIN_REL_QUAL 50
59 :     #define DEFAULT_ALT_CURVE_USE_AUTO_BONUS_BIAS 1
60 :     #define DEFAULT_ALT_CURVE_BONUS_BIAS 50
61 :    
62 :     /* Keyframe settings */
63 :     #define DEFAULT_KFTRESHOLD 10
64 :     #define DEFAULT_KFREDUCTION 20
65 :     #define DEFAULT_MIN_KEY_INTERVAL 1
66 :    
67 :     /*****************************************************************************
68 :     * Structures
69 :     ****************************************************************************/
70 : suxen_drol 1.1.2.1
71 : edgomez 1.1.2.11 /* Statistics */
72 : suxen_drol 1.1.2.1 typedef struct {
73 :     int type; /* first pass type */
74 :     int quant; /* first pass quant */
75 : suxen_drol 1.1.2.2 int blks[3]; /* k,m,y blks */
76 : suxen_drol 1.1.2.1 int length; /* first pass length */
77 : suxen_drol 1.1.2.4 int scaled_length; /* scaled length */
78 :     int desired_length; /* desired length; calcuated during encoding */
79 :    
80 :     int zone_mode; /* XVID_ZONE_xxx */
81 :     double weight;
82 : suxen_drol 1.1.2.1 } stat_t;
83 :    
84 : edgomez 1.1.2.11 /* Context struct */
85 : suxen_drol 1.1.2.1 typedef struct
86 :     {
87 :     xvid_plugin_2pass2_t param;
88 :    
89 :     /* constant statistical data */
90 : suxen_drol 1.1.2.2 int num_frames;
91 : suxen_drol 1.1.2.1 int num_keyframes;
92 : suxen_drol 1.1.2.4 uint64_t target; /* target filesize */
93 : suxen_drol 1.1.2.2
94 : suxen_drol 1.1.2.1 int count[3]; /* count of each frame types */
95 :     uint64_t tot_length[3]; /* total length of each frame types */
96 :     double avg_length[3]; /* avg */
97 :     int min_length[3]; /* min frame length of each frame types */
98 :     uint64_t tot_scaled_length[3]; /* total scaled length of each frame type */
99 :     int max_length; /* max frame size */
100 : suxen_drol 1.1.2.4
101 :     /* zone statistical data */
102 :     double avg_weight; /* average weight */
103 :     int64_t tot_quant; /* total length used by XVID_ZONE_QUANT zones */
104 :    
105 :    
106 : suxen_drol 1.1.2.1 double curve_comp_scale;
107 :     double movie_curve;
108 :    
109 :     /* dynamic */
110 :    
111 :     int * keyframe_locations;
112 :     stat_t * stats;
113 :    
114 :     double pquant_error[32];
115 :     double bquant_error[32];
116 :     int quant_count[32];
117 :     int last_quant[3];
118 :    
119 :     double curve_comp_error;
120 :     int overflow;
121 :     int KFoverflow;
122 :     int KFoverflow_partial;
123 :     int KF_idx;
124 : suxen_drol 1.1.2.4
125 :     double fq_error;
126 : suxen_drol 1.1.2.1 } rc_2pass2_t;
127 :    
128 :    
129 : edgomez 1.1.2.11 /*****************************************************************************
130 :     * Sub plugin functions prototypes
131 :     ****************************************************************************/
132 :    
133 :     static int rc_2pass2_create(xvid_plg_create_t * create, rc_2pass2_t ** handle);
134 :     static int rc_2pass2_before(rc_2pass2_t * rc, xvid_plg_data_t * data);
135 :     static int rc_2pass2_after(rc_2pass2_t * rc, xvid_plg_data_t * data);
136 :     static int rc_2pass2_destroy(rc_2pass2_t * rc, xvid_plg_destroy_t * destroy);
137 :    
138 :     /*****************************************************************************
139 :     * Plugin definition
140 :     ****************************************************************************/
141 : suxen_drol 1.1.2.1
142 : edgomez 1.1.2.11 int
143 :     xvid_plugin_2pass2(void * handle, int opt, void * param1, void * param2)
144 : suxen_drol 1.1.2.1 {
145 : edgomez 1.1.2.11 switch(opt) {
146 :     case XVID_PLG_INFO :
147 :     return 0;
148 : suxen_drol 1.1.2.1
149 : edgomez 1.1.2.11 case XVID_PLG_CREATE :
150 :     return rc_2pass2_create((xvid_plg_create_t*)param1, param2);
151 : suxen_drol 1.1.2.1
152 : edgomez 1.1.2.11 case XVID_PLG_DESTROY :
153 :     return rc_2pass2_destroy((rc_2pass2_t*)handle, (xvid_plg_destroy_t*)param1);
154 : suxen_drol 1.1.2.1
155 : edgomez 1.1.2.11 case XVID_PLG_BEFORE :
156 :     return rc_2pass2_before((rc_2pass2_t*)handle, (xvid_plg_data_t*)param1);
157 : suxen_drol 1.1.2.1
158 : edgomez 1.1.2.11 case XVID_PLG_AFTER :
159 :     return rc_2pass2_after((rc_2pass2_t*)handle, (xvid_plg_data_t*)param1);
160 :     }
161 : suxen_drol 1.1.2.1
162 : edgomez 1.1.2.11 return XVID_ERR_FAIL;
163 : suxen_drol 1.1.2.1 }
164 :    
165 : edgomez 1.1.2.11 /*****************************************************************************
166 :     * Sub plugin functions definitions
167 :     ****************************************************************************/
168 :    
169 :     /* First a few local helping function prototypes */
170 :     static int det_stats_length(rc_2pass2_t * rc, char * filename);
171 :     static int load_stats(rc_2pass2_t *rc, char * filename);
172 :     static void zone_process(rc_2pass2_t *rc, const xvid_plg_create_t * create);
173 :     static void internal_scale(rc_2pass2_t *rc);
174 :     static void pre_process0(rc_2pass2_t * rc);
175 :     static void pre_process1(rc_2pass2_t * rc);
176 : suxen_drol 1.1.2.1
177 : edgomez 1.1.2.11 /*----------------------------------------------------------------------------
178 :     *--------------------------------------------------------------------------*/
179 : suxen_drol 1.1.2.1
180 : edgomez 1.1.2.11 static int
181 :     rc_2pass2_create(xvid_plg_create_t * create, rc_2pass2_t **handle)
182 : suxen_drol 1.1.2.2 {
183 : edgomez 1.1.2.11 xvid_plugin_2pass2_t * param = (xvid_plugin_2pass2_t *)create->param;
184 :     rc_2pass2_t * rc;
185 :     int i;
186 : suxen_drol 1.1.2.2
187 : edgomez 1.1.2.11 rc = malloc(sizeof(rc_2pass2_t));
188 :     if (rc == NULL)
189 :     return XVID_ERR_MEMORY;
190 : suxen_drol 1.1.2.2
191 : edgomez 1.1.2.11 rc->param = *param;
192 : suxen_drol 1.1.2.2
193 : edgomez 1.1.2.11 #define _INIT(a, b) if((a) <= 0) (a) = (b)
194 :     /* Let's set our defaults if needed */
195 :     _INIT(rc->param.keyframe_boost, DEFAULT_KEYFRAME_BOOST);
196 :     _INIT(rc->param.payback_method, DEFAULT_PAYBACK_METHOD);
197 :     _INIT(rc->param.bitrate_payback_delay, DEFAULT_BITRATE_PAYBACK_DELAY);
198 :     _INIT(rc->param.curve_compression_high, DEFAULT_CURVE_COMPRESSION_HIGH);
199 :     _INIT(rc->param.curve_compression_low, DEFAULT_CURVE_COMPRESSION_LOW);
200 :     _INIT(rc->param.max_overflow_improvement, DEFAULT_MAX_OVERFLOW_IMPROVEMENT);
201 :     _INIT(rc->param.max_overflow_degradation, DEFAULT_MAX_OVERFLOW_DEGRADATION);
202 :    
203 :     /* Keyframe settings */
204 :     _INIT(rc->param.kftreshold, DEFAULT_KFTRESHOLD);
205 :     _INIT(rc->param.kfreduction, DEFAULT_KFREDUCTION);
206 :     _INIT(rc->param.min_key_interval, DEFAULT_MIN_KEY_INTERVAL);
207 :     #undef _INIT
208 : suxen_drol 1.1.2.2
209 : edgomez 1.1.2.11 /* Count frames in the stats file */
210 : edgomez 1.1.2.13 if (!det_stats_length(rc, param->filename)) {
211 : edgomez 1.1.2.11 DPRINTF(XVID_DEBUG_RC,"fopen %s failed\n", param->filename);
212 :     free(rc);
213 :     return XVID_ERR_FAIL;
214 : suxen_drol 1.1.2.2 }
215 : edgomez 1.1.2.7
216 : edgomez 1.1.2.11 /* Allocate the stats' memory */
217 :     if ((rc->stats = malloc(rc->num_frames * sizeof(stat_t))) == NULL) {
218 :     free(rc);
219 :     return XVID_ERR_MEMORY;
220 :     }
221 : edgomez 1.1.2.7
222 : edgomez 1.1.2.11 /*
223 :     * Allocate keyframes location's memory
224 :     * PS: see comment in pre_process0 for the +1 location requirement
225 :     */
226 :     if ((rc->keyframe_locations = malloc((rc->num_keyframes + 1) * sizeof(int))) == NULL) {
227 :     free(rc->stats);
228 :     free(rc);
229 :     return XVID_ERR_MEMORY;
230 :     }
231 : suxen_drol 1.1.2.2
232 : edgomez 1.1.2.11 if (!load_stats(rc, param->filename)) {
233 :     DPRINTF(XVID_DEBUG_RC,"fopen %s failed\n", param->filename);
234 :     free(rc->keyframe_locations);
235 :     free(rc->stats);
236 :     free(rc);
237 :     return XVID_ERR_FAIL;
238 :     }
239 : suxen_drol 1.1.2.2
240 : edgomez 1.1.2.11 /* pre-process our stats */
241 : suxen_drol 1.1.2.2
242 : edgomez 1.1.2.11 if (rc->num_frames < create->fbase/create->fincr) {
243 :     rc->target = rc->param.bitrate / 8; /* one second */
244 :     } else {
245 :     rc->target =
246 :     ((uint64_t)rc->param.bitrate * (uint64_t)rc->num_frames * (uint64_t)create->fincr) / \
247 :     ((uint64_t)create->fbase * 8);
248 :     }
249 : suxen_drol 1.1.2.2
250 : edgomez 1.1.2.11 DPRINTF(XVID_DEBUG_RC, "Number of frames: %d\n", rc->num_frames);
251 :     DPRINTF(XVID_DEBUG_RC, "Frame rate: %d/%d\n", create->fbase, create->fincr);
252 :     DPRINTF(XVID_DEBUG_RC, "Target bitrate: %ld\n", rc->param.bitrate);
253 :     DPRINTF(XVID_DEBUG_RC, "Target filesize: %lld\n", rc->target);
254 : suxen_drol 1.1.2.1
255 : edgomez 1.1.2.11 /* Compensate the mean frame overhead caused by the container */
256 :     rc->target -= rc->num_frames*rc->param.container_frame_overhead;
257 :     DPRINTF(XVID_DEBUG_RC, "Container Frame overhead: %d\n", rc->param.container_frame_overhead);
258 :     DPRINTF(XVID_DEBUG_RC, "Target filesize (after container compensation): %lld\n", rc->target);
259 : suxen_drol 1.1.2.1
260 : edgomez 1.1.2.11 pre_process0(rc);
261 : suxen_drol 1.1.2.1
262 : edgomez 1.1.2.11 if (rc->param.bitrate) {
263 :     zone_process(rc, create);
264 :     internal_scale(rc);
265 :     }else{
266 :     /* external scaler: ignore zone */
267 :     for (i=0;i<rc->num_frames;i++) {
268 :     rc->stats[i].zone_mode = XVID_ZONE_WEIGHT;
269 :     rc->stats[i].weight = 1.0;
270 :     }
271 :     rc->avg_weight = 1.0;
272 :     rc->tot_quant = 0;
273 : suxen_drol 1.1.2.1 }
274 : edgomez 1.1.2.11 pre_process1(rc);
275 : suxen_drol 1.1.2.1
276 : edgomez 1.1.2.11 for (i=0; i<32;i++) {
277 :     rc->pquant_error[i] = 0;
278 :     rc->bquant_error[i] = 0;
279 :     rc->quant_count[i] = 0;
280 :     }
281 : edgomez 1.1.2.9
282 : edgomez 1.1.2.11 rc->fq_error = 0;
283 :    
284 :     *handle = rc;
285 :     return(0);
286 :     }
287 : suxen_drol 1.1.2.1
288 : edgomez 1.1.2.11 /*----------------------------------------------------------------------------
289 :     *--------------------------------------------------------------------------*/
290 : suxen_drol 1.1.2.1
291 : edgomez 1.1.2.11 static int
292 :     rc_2pass2_destroy(rc_2pass2_t * rc, xvid_plg_destroy_t * destroy)
293 :     {
294 :     free(rc->keyframe_locations);
295 :     free(rc->stats);
296 :     free(rc);
297 :     return(0);
298 :     }
299 : suxen_drol 1.1.2.1
300 : edgomez 1.1.2.11 /*----------------------------------------------------------------------------
301 :     *--------------------------------------------------------------------------*/
302 :    
303 :     static int
304 :     rc_2pass2_before(rc_2pass2_t * rc, xvid_plg_data_t * data)
305 :     {
306 :     stat_t * s = &rc->stats[data->frame_num];
307 :     int overflow;
308 :     int desired;
309 :     double dbytes;
310 :     double curve_temp;
311 :     int capped_to_max_framesize = 0;
312 : edgomez 1.1.2.7
313 :     /*
314 : edgomez 1.1.2.11 * This function is quite long but easy to understand. In order to simplify
315 :     * the code path (a bit), we treat 3 cases that can return immediatly.
316 : edgomez 1.1.2.7 */
317 : edgomez 1.1.2.9
318 : edgomez 1.1.2.11 /* First case: Another plugin has already set a quantizer */
319 :     if (data->quant > 0)
320 :     return(0);
321 : suxen_drol 1.1.2.2
322 : edgomez 1.1.2.11 /* Second case: We are in a Quant zone */
323 :     if (s->zone_mode == XVID_ZONE_QUANT) {
324 : suxen_drol 1.1.2.4
325 : edgomez 1.1.2.11 rc->fq_error += s->weight;
326 :     data->quant = (int)rc->fq_error;
327 :     rc->fq_error -= data->quant;
328 :    
329 :     s->desired_length = s->length;
330 : suxen_drol 1.1.2.4
331 : edgomez 1.1.2.11 return(0);
332 : suxen_drol 1.1.2.4
333 : edgomez 1.1.2.11 }
334 : suxen_drol 1.1.2.4
335 : edgomez 1.1.2.11 /* Third case: insufficent stats data */
336 :     if (data->frame_num >= rc->num_frames)
337 :     return 0;
338 : suxen_drol 1.1.2.4
339 : edgomez 1.1.2.11 /*
340 :     * The last case is the one every normal minded developer should fear to
341 :     * maintain in a project :-)
342 :     */
343 : suxen_drol 1.1.2.4
344 : edgomez 1.1.2.11 /* XXX: why by 8 */
345 :     overflow = rc->overflow / 8;
346 : suxen_drol 1.1.2.4
347 : edgomez 1.1.2.11 /*
348 :     * The rc->overflow field represents the overflow in current scene (between two
349 :     * IFrames) so we must not forget to reset it if we are entering a new scene
350 :     */
351 : edgomez 1.1.2.13 if (s->type == XVID_TYPE_IVOP)
352 : edgomez 1.1.2.11 overflow = 0;
353 : suxen_drol 1.1.2.4
354 : edgomez 1.1.2.11 desired = s->scaled_length;
355 : suxen_drol 1.1.2.4
356 : edgomez 1.1.2.11 dbytes = desired;
357 : edgomez 1.1.2.13 if (s->type == XVID_TYPE_IVOP)
358 : edgomez 1.1.2.11 dbytes += desired * rc->param.keyframe_boost / 100;
359 :     dbytes /= rc->movie_curve;
360 : suxen_drol 1.1.2.4
361 : edgomez 1.1.2.11 /*
362 :     * We are now entering in the hard part of the algo, it was first designed
363 :     * to work with i/pframes only streams, so the way it computes things is
364 :     * adapted to pframes only. However we can use it if we just take care to
365 :     * scale the bframes sizes to pframes sizes using the ratio avg_p/avg_p and
366 :     * then before really using values depending on frame sizes, scaling the
367 :     * value again with the inverse ratio
368 :     */
369 :     if (s->type == XVID_TYPE_BVOP)
370 :     dbytes *= rc->avg_length[XVID_TYPE_PVOP-1] / rc->avg_length[XVID_TYPE_BVOP-1];
371 : suxen_drol 1.1.2.4
372 : edgomez 1.1.2.11 /*
373 :     * Apply user's choosen Payback method. Payback helps bitrate to follow the
374 :     * scaled curve "paying back" past errors in curve previsions.
375 :     */
376 :     if (rc->param.payback_method == XVID_PAYBACK_BIAS) {
377 :     desired =(int)(rc->curve_comp_error / rc->param.bitrate_payback_delay);
378 :     } else {
379 :     desired = (int)(rc->curve_comp_error * dbytes /
380 :     rc->avg_length[XVID_TYPE_PVOP-1] / rc->param.bitrate_payback_delay);
381 : suxen_drol 1.1.2.4
382 : edgomez 1.1.2.11 if (labs(desired) > fabs(rc->curve_comp_error)) {
383 :     desired = (int)rc->curve_comp_error;
384 :     }
385 :     }
386 : suxen_drol 1.1.2.4
387 : edgomez 1.1.2.11 rc->curve_comp_error -= desired;
388 : suxen_drol 1.1.2.4
389 : edgomez 1.1.2.9 /*
390 : edgomez 1.1.2.11 * Alt curve treatment is not that hard to understand though the formulas
391 :     * seem to be huge. Alt treatment is basically a way to soft/harden the
392 :     * curve flux applying sine/linear/cosine ratios
393 : edgomez 1.1.2.9 */
394 : suxen_drol 1.1.2.4
395 : edgomez 1.1.2.11 /* XXX: warning */
396 :     curve_temp = 0;
397 : suxen_drol 1.1.2.4
398 : edgomez 1.1.2.13 if ((rc->param.curve_compression_high + rc->param.curve_compression_low) && s->type != XVID_TYPE_IVOP) {
399 : suxen_drol 1.1.2.4
400 : edgomez 1.1.2.11 curve_temp = rc->curve_comp_scale;
401 :     if (dbytes > rc->avg_length[XVID_TYPE_PVOP-1]) {
402 :     curve_temp *= ((double)dbytes + (rc->avg_length[XVID_TYPE_PVOP-1] - dbytes) * rc->param.curve_compression_high / 100.0);
403 :     } else {
404 :     curve_temp *= ((double)dbytes + (rc->avg_length[XVID_TYPE_PVOP-1] - dbytes) * rc->param.curve_compression_low / 100.0);
405 :     }
406 : suxen_drol 1.1.2.4
407 : edgomez 1.1.2.11 /*
408 :     * End of code path for curve_temp, as told earlier, we are now
409 :     * obliged to scale the value to a bframe one using the inverse
410 :     * ratio applied earlier
411 :     */
412 :     if (s->type == XVID_TYPE_BVOP)
413 :     curve_temp *= rc->avg_length[XVID_TYPE_BVOP-1] / rc->avg_length[XVID_TYPE_PVOP-1];
414 : suxen_drol 1.1.2.4
415 : edgomez 1.1.2.11 desired += (int)curve_temp;
416 :     rc->curve_comp_error += curve_temp - (int)curve_temp;
417 :     } else {
418 :     /*
419 :     * End of code path for dbytes, as told earlier, we are now
420 :     * obliged to scale the value to a bframe one using the inverse
421 :     * ratio applied earlier
422 :     */
423 :     if (s->type == XVID_TYPE_BVOP)
424 :     dbytes *= rc->avg_length[XVID_TYPE_BVOP-1] / rc->avg_length[XVID_TYPE_PVOP-1];
425 :    
426 :     desired += (int)dbytes;
427 :     rc->curve_comp_error += dbytes - (int)dbytes;
428 : suxen_drol 1.1.2.4 }
429 :    
430 :    
431 : edgomez 1.1.2.11 /*
432 :     * We can't do bigger frames than first pass, this would be stupid as first
433 :     * pass is quant=2 and that reaching quant=1 is not worth it. We would lose
434 :     * many bytes and we would not not gain much quality.
435 :     */
436 :     if (desired > s->length) {
437 :     rc->curve_comp_error += desired - s->length;
438 :     desired = s->length;
439 :     } else {
440 :     if (desired < rc->min_length[s->type-1]) {
441 :     if (s->type == XVID_TYPE_IVOP){
442 :     rc->curve_comp_error -= rc->min_length[XVID_TYPE_IVOP-1] - desired;
443 :     }
444 :     desired = rc->min_length[s->type-1];
445 :     }
446 :     }
447 : suxen_drol 1.1.2.2
448 : edgomez 1.1.2.11 s->desired_length = desired;
449 : suxen_drol 1.1.2.2
450 : edgomez 1.1.2.11
451 :     /* if this keyframe is too close to the next, reduce it's byte allotment
452 :     XXX: why do we do this after setting the desired length */
453 : suxen_drol 1.1.2.2
454 : edgomez 1.1.2.11 if (s->type == XVID_TYPE_IVOP) {
455 :     int KFdistance = rc->keyframe_locations[rc->KF_idx] - rc->keyframe_locations[rc->KF_idx - 1];
456 : suxen_drol 1.1.2.2
457 : edgomez 1.1.2.11 if (KFdistance < rc->param.kftreshold) {
458 :    
459 :     KFdistance -= rc->param.min_key_interval;
460 : suxen_drol 1.1.2.2
461 : edgomez 1.1.2.11 if (KFdistance >= 0) {
462 :     int KF_min_size;
463 : suxen_drol 1.1.2.2
464 : edgomez 1.1.2.11 KF_min_size = desired * (100 - rc->param.kfreduction) / 100;
465 :     if (KF_min_size < 1)
466 :     KF_min_size = 1;
467 : suxen_drol 1.1.2.1
468 : edgomez 1.1.2.11 desired = KF_min_size + (desired - KF_min_size) * KFdistance /
469 :     (rc->param.kftreshold - rc->param.min_key_interval);
470 : suxen_drol 1.1.2.1
471 : edgomez 1.1.2.11 if (desired < 1)
472 :     desired = 1;
473 :     }
474 :     }
475 :     }
476 : suxen_drol 1.1.2.1
477 : edgomez 1.1.2.11 overflow = (int)((double)overflow * desired / rc->avg_length[XVID_TYPE_PVOP-1]);
478 : suxen_drol 1.1.2.1
479 : edgomez 1.1.2.11 /* Reign in overflow with huge frames */
480 : edgomez 1.1.2.13 if (labs(overflow) > labs(rc->overflow))
481 : edgomez 1.1.2.11 overflow = rc->overflow;
482 : suxen_drol 1.1.2.1
483 : edgomez 1.1.2.11 /* Make sure overflow doesn't run away */
484 :     if (overflow > desired * rc->param.max_overflow_improvement / 100) {
485 :     desired += (overflow <= desired) ? desired * rc->param.max_overflow_improvement / 100 :
486 :     overflow * rc->param.max_overflow_improvement / 100;
487 :     } else if (overflow < desired * rc->param.max_overflow_degradation / -100){
488 :     desired += desired * rc->param.max_overflow_degradation / -100;
489 :     } else {
490 :     desired += overflow;
491 :     }
492 : suxen_drol 1.1.2.1
493 : edgomez 1.1.2.11 /* Make sure we are not higher than desired frame size */
494 :     if (desired > rc->max_length) {
495 :     capped_to_max_framesize = 1;
496 :     desired = rc->max_length;
497 :     DPRINTF(XVID_DEBUG_RC,"[%i] Capped to maximum frame size\n",
498 :     data->frame_num);
499 :     }
500 : suxen_drol 1.1.2.1
501 : edgomez 1.1.2.11 /* Make sure to not scale below the minimum framesize */
502 :     if (desired < rc->min_length[s->type-1]) {
503 :     desired = rc->min_length[s->type-1];
504 :     DPRINTF(XVID_DEBUG_RC,"[%i] Capped to minimum frame size\n",
505 :     data->frame_num);
506 :     }
507 : suxen_drol 1.1.2.1
508 : edgomez 1.1.2.11 /*
509 :     * Don't laugh at this very 'simple' quant<->filesize relationship, it
510 :     * proves to be acurate enough for our algorithm
511 :     */
512 :     data->quant = s->quant*s->length/desired;
513 : suxen_drol 1.1.2.1
514 : edgomez 1.1.2.11 /* Let's clip the computed quantizer, if needed */
515 :     if (data->quant < 1) {
516 :     data->quant = 1;
517 :     } else if (data->quant > 31) {
518 :     data->quant = 31;
519 :     } else if (s->type != XVID_TYPE_IVOP) {
520 : suxen_drol 1.1.2.1
521 : edgomez 1.1.2.11 /*
522 :     * The frame quantizer has not been clipped, this appear to be a good
523 :     * computed quantizer, however past frames give us some info about how
524 :     * this quantizer performs against the algo prevision. Let's use this
525 :     * prevision to increase the quantizer when we observe a too big
526 :     * accumulated error
527 :     */
528 :     if (s->type == XVID_TYPE_BVOP) {
529 :     rc->bquant_error[data->quant] += ((double)(s->quant * s->length) / desired) - data->quant;
530 : suxen_drol 1.1.2.1
531 : edgomez 1.1.2.11 if (rc->bquant_error[data->quant] >= 1.0) {
532 :     rc->bquant_error[data->quant] -= 1.0;
533 :     data->quant++;
534 :     }
535 :     } else {
536 :     rc->pquant_error[data->quant] += ((double)(s->quant * s->length) / desired) - data->quant;
537 : suxen_drol 1.1.2.1
538 : edgomez 1.1.2.11 if (rc->pquant_error[data->quant] >= 1.0) {
539 :     rc->pquant_error[data->quant] -= 1.0;
540 :     data->quant++;
541 :     }
542 :     }
543 :     }
544 : suxen_drol 1.1.2.1
545 : edgomez 1.1.2.11 /*
546 :     * Now we have a computed quant that is in the right quante range, with a
547 :     * possible +1 correction due to cumulated error. We can now safely clip
548 :     * the quantizer again with user's quant ranges. "Safely" means the Rate
549 :     * Control could learn more about this quantizer, this knowledge is useful
550 :     * for future frames even if it this quantizer won't be really used atm,
551 :     * that's why we don't perform this clipping earlier.
552 :     */
553 :     if (data->quant < data->min_quant[s->type-1]) {
554 :     data->quant = data->min_quant[s->type-1];
555 :     } else if (data->quant > data->max_quant[s->type-1]) {
556 :     data->quant = data->max_quant[s->type-1];
557 :     }
558 : suxen_drol 1.1.2.1
559 : edgomez 1.1.2.11 /*
560 :     * To avoid big quality jumps from frame to frame, we apply a "security"
561 :     * rule that makes |last_quant - new_quant| <= 2. This rule only applies
562 :     * to predicted frames (P and B)
563 :     */
564 :     if (s->type != XVID_TYPE_IVOP && rc->last_quant[s->type-1] && capped_to_max_framesize == 0) {
565 : suxen_drol 1.1.2.1
566 : edgomez 1.1.2.11 if (data->quant > rc->last_quant[s->type-1] + 2) {
567 :     data->quant = rc->last_quant[s->type-1] + 2;
568 :     DPRINTF(XVID_DEBUG_RC,
569 :     "[%i] p/b-frame quantizer prevented from rising too steeply\n",
570 :     data->frame_num);
571 :     }
572 :     if (data->quant < rc->last_quant[s->type-1] - 2) {
573 :     data->quant = rc->last_quant[s->type-1] - 2;
574 :     DPRINTF(XVID_DEBUG_RC,
575 :     "[%i] p/b-frame quantizer prevented from falling too steeply\n",
576 :     data->frame_num);
577 :     }
578 :     }
579 : suxen_drol 1.1.2.1
580 : edgomez 1.1.2.11 /*
581 :     * We don't want to pollute the RC history results when our computed quant
582 :     * has been computed from a capped frame size
583 :     */
584 : edgomez 1.1.2.13 if (capped_to_max_framesize == 0)
585 : edgomez 1.1.2.11 rc->last_quant[s->type-1] = data->quant;
586 : suxen_drol 1.1.2.1
587 : edgomez 1.1.2.11 return 0;
588 :     }
589 : suxen_drol 1.1.2.1
590 : edgomez 1.1.2.11 /*----------------------------------------------------------------------------
591 :     *--------------------------------------------------------------------------*/
592 : suxen_drol 1.1.2.1
593 : edgomez 1.1.2.11 static int
594 :     rc_2pass2_after(rc_2pass2_t * rc, xvid_plg_data_t * data)
595 :     {
596 : edgomez 1.1.2.12 const char frame_type[4] = { 'i', 'p', 'b', 's'};
597 :     stat_t * s = &rc->stats[data->frame_num];
598 : suxen_drol 1.1.2.1
599 : edgomez 1.1.2.11 /* Insufficent stats data */
600 :     if (data->frame_num >= rc->num_frames)
601 :     return 0;
602 : suxen_drol 1.1.2.1
603 : edgomez 1.1.2.11 rc->quant_count[data->quant]++;
604 : suxen_drol 1.1.2.1
605 : edgomez 1.1.2.11 if (data->type == XVID_TYPE_IVOP) {
606 :     int kfdiff = (rc->keyframe_locations[rc->KF_idx] - rc->keyframe_locations[rc->KF_idx - 1]);
607 : suxen_drol 1.1.2.1
608 : edgomez 1.1.2.11 rc->overflow += rc->KFoverflow;
609 :     rc->KFoverflow = s->desired_length - data->length;
610 :    
611 :     if (kfdiff > 1) { // non-consecutive keyframes
612 :     rc->KFoverflow_partial = rc->KFoverflow / (kfdiff - 1);
613 :     }else{ // consecutive keyframes
614 :     rc->overflow += rc->KFoverflow;
615 :     rc->KFoverflow = 0;
616 :     rc->KFoverflow_partial = 0;
617 :     }
618 :     rc->KF_idx++;
619 :     } else {
620 :     // distribute part of the keyframe overflow
621 :     rc->overflow += s->desired_length - data->length + rc->KFoverflow_partial;
622 :     rc->KFoverflow -= rc->KFoverflow_partial;
623 :     }
624 : suxen_drol 1.1.2.1
625 : edgomez 1.1.2.12 DPRINTF(XVID_DEBUG_RC, "[%i] type:%c quant:%i stats1:%i scaled:%i actual:%i overflow:%i\n",
626 : edgomez 1.1.2.11 data->frame_num,
627 : edgomez 1.1.2.12 frame_type[data->type-1],
628 : edgomez 1.1.2.11 data->quant,
629 :     s->length,
630 :     s->scaled_length,
631 :     data->length,
632 :     rc->overflow);
633 : suxen_drol 1.1.2.1
634 : edgomez 1.1.2.11 return(0);
635 : suxen_drol 1.1.2.1 }
636 :    
637 : edgomez 1.1.2.11 /*****************************************************************************
638 :     * Helper functions definition
639 :     ****************************************************************************/
640 : suxen_drol 1.1.2.1
641 : edgomez 1.1.2.11 #define BUF_SZ 1024
642 :     #define MAX_COLS 5
643 : suxen_drol 1.1.2.1
644 : edgomez 1.1.2.11 /* open stats file, and count num frames */
645 :     static int
646 :     det_stats_length(rc_2pass2_t * rc, char * filename)
647 : suxen_drol 1.1.2.1 {
648 : edgomez 1.1.2.11 FILE * f;
649 :     int n, ignore;
650 :     char type;
651 : suxen_drol 1.1.2.1
652 : edgomez 1.1.2.11 rc->num_frames = 0;
653 :     rc->num_keyframes = 0;
654 : suxen_drol 1.1.2.1
655 : edgomez 1.1.2.11 if ((f = fopen(filename, "rt")) == NULL)
656 :     return 0;
657 : suxen_drol 1.1.2.1
658 : edgomez 1.1.2.11 while((n = fscanf(f, "%c %d %d %d %d %d %d\n",
659 :     &type, &ignore, &ignore, &ignore, &ignore, &ignore, &ignore)) != EOF) {
660 :     if (type == 'i') {
661 :     rc->num_frames++;
662 :     rc->num_keyframes++;
663 :     }else if (type == 'p' || type == 'b' || type == 's') {
664 :     rc->num_frames++;
665 :     }
666 : suxen_drol 1.1.2.1 }
667 :    
668 : edgomez 1.1.2.11 fclose(f);
669 : suxen_drol 1.1.2.1
670 : edgomez 1.1.2.11 return 1;
671 :     }
672 : suxen_drol 1.1.2.2
673 : edgomez 1.1.2.11 /* open stats file(s) and read into rc->stats array */
674 : suxen_drol 1.1.2.4
675 : edgomez 1.1.2.11 static int
676 :     load_stats(rc_2pass2_t *rc, char * filename)
677 :     {
678 :     FILE * f;
679 :     int i, not_scaled;
680 :    
681 : suxen_drol 1.1.2.5
682 : edgomez 1.1.2.11 if ((f = fopen(filename, "rt"))==NULL)
683 :     return 0;
684 :    
685 :     i = 0;
686 :     not_scaled = 0;
687 :     while(i < rc->num_frames) {
688 :     stat_t * s = &rc->stats[i];
689 :     int n;
690 :     char type;
691 : suxen_drol 1.1.2.2
692 : edgomez 1.1.2.11 s->scaled_length = 0;
693 :     n = fscanf(f, "%c %d %d %d %d %d %d\n", &type, &s->quant, &s->blks[0], &s->blks[1], &s->blks[2], &s->length, &s->scaled_length);
694 :     if (n == EOF) break;
695 :     if (n < 7) {
696 :     not_scaled = 1;
697 :     }
698 : edgomez 1.1.2.6
699 : edgomez 1.1.2.11 if (type == 'i') {
700 :     s->type = XVID_TYPE_IVOP;
701 :     }else if (type == 'p' || type == 's') {
702 :     s->type = XVID_TYPE_PVOP;
703 :     }else if (type == 'b') {
704 :     s->type = XVID_TYPE_BVOP;
705 :     }else{ /* unknown type */
706 :     DPRINTF(XVID_DEBUG_RC, "unknown stats frame type; assuming pvop\n");
707 :     s->type = XVID_TYPE_PVOP;
708 : suxen_drol 1.1.2.4 }
709 :    
710 : edgomez 1.1.2.11 i++;
711 : suxen_drol 1.1.2.4 }
712 :    
713 : edgomez 1.1.2.11 rc->num_frames = i;
714 : suxen_drol 1.1.2.1
715 : edgomez 1.1.2.11 fclose(f);
716 : suxen_drol 1.1.2.1
717 : edgomez 1.1.2.11 return 1;
718 : suxen_drol 1.1.2.1 }
719 :    
720 : edgomez 1.1.2.11 #if 0
721 :     static void print_stats(rc_2pass2_t * rc)
722 :     {
723 :     int i;
724 :     DPRINTF(XVID_DEBUG_RC, "type quant length scaled_length\n");
725 :     for (i = 0; i < rc->num_frames; i++) {
726 :     stat_t * s = &rc->stats[i];
727 :     DPRINTF(XVID_DEBUG_RC, "%d %d %d %d\n", s->type, s->quant, s->length, s->scaled_length);
728 :     }
729 :     }
730 :     #endif
731 : suxen_drol 1.1.2.1
732 : edgomez 1.1.2.11 /* pre-process the statistics data
733 :     - for each type, count, tot_length, min_length, max_length
734 :     - set keyframes_locations
735 :     */
736 : suxen_drol 1.1.2.1
737 : edgomez 1.1.2.11 static void
738 :     pre_process0(rc_2pass2_t * rc)
739 : suxen_drol 1.1.2.1 {
740 : edgomez 1.1.2.11 int i,j;
741 : edgomez 1.1.2.7
742 : edgomez 1.1.2.11 for (i=0; i<3; i++) {
743 :     rc->count[i]=0;
744 :     rc->tot_length[i] = 0;
745 :     rc->last_quant[i] = 0;
746 :     rc->min_length[i] = INT_MAX;
747 :     }
748 : edgomez 1.1.2.7
749 : edgomez 1.1.2.11 rc->max_length = INT_MIN;
750 : edgomez 1.1.2.7
751 : edgomez 1.1.2.11 for (i=j=0; i<rc->num_frames; i++) {
752 :     stat_t * s = &rc->stats[i];
753 : suxen_drol 1.1.2.1
754 : edgomez 1.1.2.11 rc->count[s->type-1]++;
755 :     rc->tot_length[s->type-1] += s->length;
756 :    
757 :     if (s->length < rc->min_length[s->type-1]) {
758 :     rc->min_length[s->type-1] = s->length;
759 :     }
760 : suxen_drol 1.1.2.1
761 : edgomez 1.1.2.11 if (s->length > rc->max_length) {
762 :     rc->max_length = s->length;
763 :     }
764 : suxen_drol 1.1.2.1
765 : edgomez 1.1.2.11 if (s->type == XVID_TYPE_IVOP) {
766 :     rc->keyframe_locations[j] = i;
767 :     j++;
768 :     }
769 :     }
770 : edgomez 1.1.2.7
771 :     /*
772 : edgomez 1.1.2.11 * Nota Bene:
773 :     * The "per sequence" overflow system considers a natural sequence to be
774 :     * formed by all frames between two iframes, so if we want to make sure
775 :     * the system does not go nuts during last sequence, we force the last
776 :     * frame to appear in the keyframe locations array.
777 : edgomez 1.1.2.7 */
778 : edgomez 1.1.2.11 rc->keyframe_locations[j] = i;
779 : edgomez 1.1.2.7
780 : edgomez 1.1.2.11 DPRINTF(XVID_DEBUG_RC, "Min 1st pass IFrame length: %d\n", rc->min_length[0]);
781 :     DPRINTF(XVID_DEBUG_RC, "Min 1st pass PFrame length: %d\n", rc->min_length[1]);
782 :     DPRINTF(XVID_DEBUG_RC, "Min 1st pass BFrame length: %d\n", rc->min_length[2]);
783 :     }
784 :    
785 :     /* calculate zone weight "center" */
786 : edgomez 1.1.2.7
787 : edgomez 1.1.2.11 static void
788 :     zone_process(rc_2pass2_t *rc, const xvid_plg_create_t * create)
789 :     {
790 :     int i,j;
791 :     int n = 0;
792 : suxen_drol 1.1.2.5
793 : edgomez 1.1.2.11 rc->avg_weight = 0.0;
794 :     rc->tot_quant = 0;
795 : suxen_drol 1.1.2.1
796 :    
797 : edgomez 1.1.2.11 if (create->num_zones == 0) {
798 :     for (j = 0; j < rc->num_frames; j++) {
799 :     rc->stats[j].zone_mode = XVID_ZONE_WEIGHT;
800 :     rc->stats[j].weight = 1.0;
801 :     }
802 :     rc->avg_weight += rc->num_frames * 1.0;
803 :     n += rc->num_frames;
804 :     }
805 :    
806 :    
807 :     for(i=0; i < create->num_zones; i++) {
808 :    
809 :     int next = (i+1<create->num_zones) ? create->zones[i+1].frame : rc->num_frames;
810 :    
811 :     if (i==0 && create->zones[i].frame > 0) {
812 :     for (j = 0; j < create->zones[i].frame && j < rc->num_frames; j++) {
813 :     rc->stats[j].zone_mode = XVID_ZONE_WEIGHT;
814 :     rc->stats[j].weight = 1.0;
815 :     }
816 :     rc->avg_weight += create->zones[i].frame * 1.0;
817 :     n += create->zones[i].frame;
818 :     }
819 :    
820 :     if (create->zones[i].mode == XVID_ZONE_WEIGHT) {
821 :     for (j = create->zones[i].frame; j < next && j < rc->num_frames; j++ ) {
822 :     rc->stats[j].zone_mode = XVID_ZONE_WEIGHT;
823 :     rc->stats[j].weight = (double)create->zones[i].increment / (double)create->zones[i].base;
824 :     }
825 :     next -= create->zones[i].frame;
826 :     rc->avg_weight += (double)(next * create->zones[i].increment) / (double)create->zones[i].base;
827 :     n += next;
828 :     }else{ // XVID_ZONE_QUANT
829 :     for (j = create->zones[i].frame; j < next && j < rc->num_frames; j++ ) {
830 :     rc->stats[j].zone_mode = XVID_ZONE_QUANT;
831 :     rc->stats[j].weight = (double)create->zones[i].increment / (double)create->zones[i].base;
832 :     rc->tot_quant += rc->stats[j].length;
833 :     }
834 :     }
835 :     }
836 :     rc->avg_weight = n>0 ? rc->avg_weight/n : 1.0;
837 : suxen_drol 1.1.2.1
838 : edgomez 1.1.2.11 DPRINTF(XVID_DEBUG_RC, "center_weight: %f (for %i frames); fixed_bytes: %i\n", rc->avg_weight, n, rc->tot_quant);
839 :     }
840 : suxen_drol 1.1.2.1
841 :    
842 : edgomez 1.1.2.11 /* scale the curve */
843 : suxen_drol 1.1.2.1
844 : edgomez 1.1.2.11 static void
845 :     internal_scale(rc_2pass2_t *rc)
846 :     {
847 :     int64_t target = rc->target - rc->tot_quant;
848 :     int64_t pass1_length = rc->tot_length[0] + rc->tot_length[1] + rc->tot_length[2] - rc->tot_quant;
849 :     double scaler;
850 :     int i;
851 : suxen_drol 1.1.2.1
852 :    
853 : edgomez 1.1.2.11 /* Let's compute a linear scaler in order to perform curve scaling */
854 :     scaler = (double)target / (double)pass1_length;
855 : suxen_drol 1.1.2.1
856 : edgomez 1.1.2.11 if (target <= 0 || pass1_length <= 0 || target >= pass1_length) {
857 :     DPRINTF(XVID_DEBUG_RC, "WARNING: Undersize detected\n");
858 :     scaler = 1.0;
859 :     }
860 : edgomez 1.1.2.7
861 : edgomez 1.1.2.11 DPRINTF(XVID_DEBUG_RC,
862 :     "Before correction: target=%i, tot_length=%i, scaler=%f\n",
863 :     (int)target, (int)pass1_length, scaler);
864 : edgomez 1.1.2.7
865 : edgomez 1.1.2.11 /*
866 :     * Perform an initial scale pass.
867 :     * If a frame size is scaled underneath our hardcoded minimums, then we
868 :     * force the frame size to the minimum, and deduct the original & scaled
869 :     * frame length from the original and target total lengths
870 :     */
871 : suxen_drol 1.1.2.1
872 : edgomez 1.1.2.11 for (i=0; i<rc->num_frames; i++) {
873 :     stat_t * s = &rc->stats[i];
874 :     int min_size[3];
875 :     int len;
876 : suxen_drol 1.1.2.1
877 : edgomez 1.1.2.11 /* Compute min frame lengths (oe for each frame type) */
878 :     min_size[0] = ((s->blks[0]*22) + 240) / 8;
879 :     min_size[1] = (s->blks[0] + 88) / 8;
880 :     min_size[2] = 8;
881 : suxen_drol 1.1.2.1
882 : edgomez 1.1.2.11 if (s->zone_mode == XVID_ZONE_QUANT) {
883 :     s->scaled_length = s->length;
884 :     continue;
885 : edgomez 1.1.2.7 }
886 : suxen_drol 1.1.2.1
887 : edgomez 1.1.2.11 /* Compute teh scaled length */
888 :     len = (int)((double)s->length * scaler * s->weight / rc->avg_weight);
889 : edgomez 1.1.2.7
890 : edgomez 1.1.2.11 /* Compare with the computed minimum */
891 :     if (len < min_size[s->type-1]) {
892 :     /* force frame size to our computed minimum */
893 :     s->scaled_length = min_size[s->type-1];
894 :     target -= s->scaled_length;
895 :     pass1_length -= s->length;
896 :     } else {
897 :     /* Do nothing for now, we'll scale this later */
898 :     s->scaled_length = 0;
899 : edgomez 1.1.2.7 }
900 : edgomez 1.1.2.11
901 : edgomez 1.1.2.7 }
902 : suxen_drol 1.1.2.1
903 : edgomez 1.1.2.11 /* Correct the scaler for all non forced frames */
904 :     scaler = (double)target / (double)pass1_length;
905 : suxen_drol 1.1.2.5
906 : edgomez 1.1.2.11 /* Detect undersizing */
907 :     if (target <= 0 || pass1_length <= 0 || target >= pass1_length) {
908 :     DPRINTF(XVID_DEBUG_RC, "WARNING: Undersize detected\n");
909 :     scaler = 1.0;
910 : edgomez 1.1.2.7 }
911 : suxen_drol 1.1.2.1
912 : edgomez 1.1.2.11 DPRINTF(XVID_DEBUG_RC,
913 :     "After correction: target=%i, tot_length=%i, scaler=%f\n",
914 :     (int)target, (int)pass1_length, scaler);
915 : suxen_drol 1.1.2.4
916 : edgomez 1.1.2.11 /* Do another pass with the new scaler */
917 :     for (i=0; i<rc->num_frames; i++) {
918 :     stat_t * s = &rc->stats[i];
919 : suxen_drol 1.1.2.4
920 : edgomez 1.1.2.11 /* Ignore frame with forced frame sizes */
921 :     if (s->scaled_length == 0)
922 :     s->scaled_length = (int)((double)s->length * scaler * s->weight / rc->avg_weight);
923 :     }
924 : edgomez 1.1.2.7
925 : edgomez 1.1.2.11 }
926 : suxen_drol 1.1.2.1
927 : edgomez 1.1.2.11 static void
928 :     pre_process1(rc_2pass2_t * rc)
929 :     {
930 :     int i;
931 :     double total1, total2;
932 :     uint64_t ivop_boost_total;
933 : suxen_drol 1.1.2.1
934 : edgomez 1.1.2.11 ivop_boost_total = 0;
935 :     rc->curve_comp_error = 0;
936 : suxen_drol 1.1.2.1
937 : edgomez 1.1.2.11 for (i=0; i<3; i++) {
938 :     rc->tot_scaled_length[i] = 0;
939 :     }
940 : suxen_drol 1.1.2.1
941 : edgomez 1.1.2.11 for (i=0; i<rc->num_frames; i++) {
942 :     stat_t * s = &rc->stats[i];
943 : suxen_drol 1.1.2.1
944 : edgomez 1.1.2.11 rc->tot_scaled_length[s->type-1] += s->scaled_length;
945 :    
946 :     if (s->type == XVID_TYPE_IVOP) {
947 :     ivop_boost_total += s->scaled_length * rc->param.keyframe_boost / 100;
948 :     }
949 :     }
950 : suxen_drol 1.1.2.1
951 : edgomez 1.1.2.11 rc->movie_curve = ((double)(rc->tot_scaled_length[XVID_TYPE_PVOP-1] + rc->tot_scaled_length[XVID_TYPE_BVOP-1] + ivop_boost_total) /
952 :     (rc->tot_scaled_length[XVID_TYPE_PVOP-1] + rc->tot_scaled_length[XVID_TYPE_BVOP-1]));
953 : edgomez 1.1.2.7
954 : edgomez 1.1.2.11 for(i=0; i<3; i++) {
955 :     if (rc->count[i] == 0 || rc->movie_curve == 0) {
956 :     rc->avg_length[i] = 1;
957 :     }else{
958 :     rc->avg_length[i] = rc->tot_scaled_length[i] / rc->count[i] / rc->movie_curve;
959 :     }
960 :     }
961 : edgomez 1.1.2.7
962 : edgomez 1.1.2.11 /* --- */
963 : edgomez 1.1.2.7
964 : edgomez 1.1.2.11 total1=total2=0;
965 : edgomez 1.1.2.7
966 : edgomez 1.1.2.11 for (i=0; i<rc->num_frames; i++) {
967 :     stat_t * s = &rc->stats[i];
968 : edgomez 1.1.2.7
969 : edgomez 1.1.2.11 if (s->type != XVID_TYPE_IVOP) {
970 :     double dbytes,dbytes2;
971 : suxen_drol 1.1.2.1
972 : edgomez 1.1.2.11 dbytes = s->scaled_length / rc->movie_curve;
973 :     dbytes2 = 0; /* XXX: warning */
974 :     total1 += dbytes;
975 :     if (s->type == XVID_TYPE_BVOP)
976 :     dbytes *= rc->avg_length[XVID_TYPE_PVOP-1] / rc->avg_length[XVID_TYPE_BVOP-1];
977 : suxen_drol 1.1.2.1
978 : edgomez 1.1.2.13 if (dbytes > rc->avg_length[XVID_TYPE_PVOP-1]) {
979 :     dbytes2=((double)dbytes + (rc->avg_length[XVID_TYPE_PVOP-1] - dbytes) * rc->param.curve_compression_high / 100.0);
980 :     } else {
981 :     dbytes2 = ((double)dbytes + (rc->avg_length[XVID_TYPE_PVOP-1] - dbytes) * rc->param.curve_compression_low / 100.0);
982 :     }
983 : suxen_drol 1.1.2.1
984 : edgomez 1.1.2.11 if (s->type == XVID_TYPE_BVOP) {
985 :     dbytes2 *= rc->avg_length[XVID_TYPE_BVOP-1] / rc->avg_length[XVID_TYPE_PVOP-1];
986 :     if (dbytes2 < rc->min_length[XVID_TYPE_BVOP-1])
987 :     dbytes2 = rc->min_length[XVID_TYPE_BVOP-1];
988 :     }else{
989 :     if (dbytes2 < rc->min_length[XVID_TYPE_PVOP-1])
990 :     dbytes2 = rc->min_length[XVID_TYPE_PVOP-1];
991 :     }
992 :     total2 += dbytes2;
993 : suxen_drol 1.1.2.1 }
994 :     }
995 :    
996 : edgomez 1.1.2.11 rc->curve_comp_scale = total1 / total2;
997 : suxen_drol 1.1.2.1
998 : edgomez 1.1.2.13 DPRINTF(XVID_DEBUG_RC, "middle frame size for asymmetric curve compression: %i\n",
999 : edgomez 1.1.2.11 (int)(rc->avg_length[XVID_TYPE_PVOP-1] * rc->curve_comp_scale));
1000 : suxen_drol 1.1.2.1
1001 : edgomez 1.1.2.11 rc->overflow = 0;
1002 :     rc->KFoverflow = 0;
1003 :     rc->KFoverflow_partial = 0;
1004 :     rc->KF_idx = 1;
1005 : suxen_drol 1.1.2.1 }

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