[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.21 - (view) (download)

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

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