[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.17 - (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.17 * $Id: plugin_2pass2.c,v 1.1.2.16 2003/05/29 12:38:44 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 : edgomez 1.1.2.11 if (rc->num_frames < create->fbase/create->fincr) {
248 : edgomez 1.1.2.15 /* Source sequence is less than 1s long, we do as if it was 1s long */
249 :     rc->target = rc->param.bitrate / 8;
250 : edgomez 1.1.2.11 } else {
251 : edgomez 1.1.2.15 /* Target filesize = bitrate/8 * numframes / framerate */
252 : edgomez 1.1.2.11 rc->target =
253 : edgomez 1.1.2.15 ((uint64_t)rc->param.bitrate * (uint64_t)rc->num_frames * \
254 :     (uint64_t)create->fincr) / \
255 : edgomez 1.1.2.11 ((uint64_t)create->fbase * 8);
256 :     }
257 : suxen_drol 1.1.2.2
258 : edgomez 1.1.2.15 DPRINTF(XVID_DEBUG_RC, "Frame rate: %d/%d (%ffps)\n",
259 :     create->fbase, create->fincr,
260 :     (double)create->fbase/(double)create->fincr);
261 :     DPRINTF(XVID_DEBUG_RC, "Number of frames: %d\n", rc->num_frames);
262 : edgomez 1.1.2.11 DPRINTF(XVID_DEBUG_RC, "Target bitrate: %ld\n", rc->param.bitrate);
263 :     DPRINTF(XVID_DEBUG_RC, "Target filesize: %lld\n", rc->target);
264 : suxen_drol 1.1.2.1
265 : edgomez 1.1.2.15 /* Compensate the average frame overhead caused by the container */
266 : edgomez 1.1.2.11 rc->target -= rc->num_frames*rc->param.container_frame_overhead;
267 :     DPRINTF(XVID_DEBUG_RC, "Container Frame overhead: %d\n", rc->param.container_frame_overhead);
268 :     DPRINTF(XVID_DEBUG_RC, "Target filesize (after container compensation): %lld\n", rc->target);
269 : suxen_drol 1.1.2.1
270 : edgomez 1.1.2.15 /*
271 :     * First data pre processing:
272 :     * - finds the minimum frame length for each frame type during 1st pass.
273 :     * rc->min_size[]
274 :     * - determines the maximum frame length observed (no frame type distinction).
275 :     * rc->max_size
276 :     * - count how many times each frame type has been used.
277 :     * rc->count[]
278 :     * - total bytes used per frame type
279 :     * rc->total[]
280 :     * - store keyframe location
281 :     * rc->keyframe_locations[]
282 :     */
283 : edgomez 1.1.2.11 pre_process0(rc);
284 : suxen_drol 1.1.2.1
285 : edgomez 1.1.2.15 /*
286 :     * When bitrate is not given it means it has been scaled by an external
287 :     * application
288 :     */
289 : edgomez 1.1.2.11 if (rc->param.bitrate) {
290 : edgomez 1.1.2.15 /* Apply zone settings */
291 :     zone_process(rc, create);
292 :     /* Perform curve scaling */
293 : edgomez 1.1.2.11 internal_scale(rc);
294 : edgomez 1.1.2.15 } else {
295 :     /* External scaling -- zones are ignored */
296 :     for (i=0;i<rc->num_frames;i++) {
297 :     rc->stats[i].zone_mode = XVID_ZONE_WEIGHT;
298 :     rc->stats[i].weight = 1.0;
299 :     }
300 :     rc->avg_weight = 1.0;
301 :     rc->tot_quant = 0;
302 :     }
303 : suxen_drol 1.1.2.1
304 : edgomez 1.1.2.15 pre_process1(rc);
305 : edgomez 1.1.2.9
306 : edgomez 1.1.2.15 *handle = rc;
307 : edgomez 1.1.2.11 return(0);
308 :     }
309 : suxen_drol 1.1.2.1
310 : edgomez 1.1.2.11 /*----------------------------------------------------------------------------
311 :     *--------------------------------------------------------------------------*/
312 : suxen_drol 1.1.2.1
313 : edgomez 1.1.2.11 static int
314 :     rc_2pass2_destroy(rc_2pass2_t * rc, xvid_plg_destroy_t * destroy)
315 :     {
316 :     free(rc->keyframe_locations);
317 :     free(rc->stats);
318 :     free(rc);
319 :     return(0);
320 :     }
321 : suxen_drol 1.1.2.1
322 : edgomez 1.1.2.11 /*----------------------------------------------------------------------------
323 :     *--------------------------------------------------------------------------*/
324 :    
325 :     static int
326 :     rc_2pass2_before(rc_2pass2_t * rc, xvid_plg_data_t * data)
327 :     {
328 : edgomez 1.1.2.15 stat_t * s = &rc->stats[data->frame_num];
329 :     int overflow;
330 :     int desired;
331 :     double dbytes;
332 :     double curve_temp;
333 :     double scaled_quant;
334 :     int capped_to_max_framesize = 0;
335 : edgomez 1.1.2.7
336 :     /*
337 : edgomez 1.1.2.11 * This function is quite long but easy to understand. In order to simplify
338 :     * the code path (a bit), we treat 3 cases that can return immediatly.
339 : edgomez 1.1.2.7 */
340 : edgomez 1.1.2.9
341 : edgomez 1.1.2.11 /* First case: Another plugin has already set a quantizer */
342 :     if (data->quant > 0)
343 :     return(0);
344 : suxen_drol 1.1.2.2
345 : edgomez 1.1.2.11 /* Second case: We are in a Quant zone */
346 :     if (s->zone_mode == XVID_ZONE_QUANT) {
347 :     rc->fq_error += s->weight;
348 :     data->quant = (int)rc->fq_error;
349 :     rc->fq_error -= data->quant;
350 :    
351 :     s->desired_length = s->length;
352 : suxen_drol 1.1.2.4
353 : edgomez 1.1.2.11 return(0);
354 :     }
355 : suxen_drol 1.1.2.4
356 : edgomez 1.1.2.11 /* Third case: insufficent stats data */
357 :     if (data->frame_num >= rc->num_frames)
358 :     return 0;
359 : suxen_drol 1.1.2.4
360 : edgomez 1.1.2.11 /*
361 :     * The last case is the one every normal minded developer should fear to
362 :     * maintain in a project :-)
363 :     */
364 : suxen_drol 1.1.2.4
365 : edgomez 1.1.2.11 /* XXX: why by 8 */
366 :     overflow = rc->overflow / 8;
367 : suxen_drol 1.1.2.4
368 : edgomez 1.1.2.11 /*
369 :     * The rc->overflow field represents the overflow in current scene (between two
370 :     * IFrames) so we must not forget to reset it if we are entering a new scene
371 :     */
372 : edgomez 1.1.2.13 if (s->type == XVID_TYPE_IVOP)
373 : edgomez 1.1.2.11 overflow = 0;
374 : suxen_drol 1.1.2.4
375 : edgomez 1.1.2.11 desired = s->scaled_length;
376 : suxen_drol 1.1.2.4
377 : edgomez 1.1.2.11 dbytes = desired;
378 : edgomez 1.1.2.13 if (s->type == XVID_TYPE_IVOP)
379 : edgomez 1.1.2.11 dbytes += desired * rc->param.keyframe_boost / 100;
380 :     dbytes /= rc->movie_curve;
381 : suxen_drol 1.1.2.4
382 : edgomez 1.1.2.11 /*
383 :     * We are now entering in the hard part of the algo, it was first designed
384 :     * to work with i/pframes only streams, so the way it computes things is
385 :     * adapted to pframes only. However we can use it if we just take care to
386 :     * scale the bframes sizes to pframes sizes using the ratio avg_p/avg_p and
387 :     * then before really using values depending on frame sizes, scaling the
388 :     * value again with the inverse ratio
389 :     */
390 :     if (s->type == XVID_TYPE_BVOP)
391 :     dbytes *= rc->avg_length[XVID_TYPE_PVOP-1] / rc->avg_length[XVID_TYPE_BVOP-1];
392 : suxen_drol 1.1.2.4
393 : edgomez 1.1.2.11 /*
394 :     * Apply user's choosen Payback method. Payback helps bitrate to follow the
395 :     * scaled curve "paying back" past errors in curve previsions.
396 :     */
397 :     if (rc->param.payback_method == XVID_PAYBACK_BIAS) {
398 : edgomez 1.1.2.15 desired = (int)(rc->curve_comp_error / rc->param.bitrate_payback_delay);
399 : edgomez 1.1.2.11 } else {
400 :     desired = (int)(rc->curve_comp_error * dbytes /
401 :     rc->avg_length[XVID_TYPE_PVOP-1] / rc->param.bitrate_payback_delay);
402 : suxen_drol 1.1.2.4
403 : edgomez 1.1.2.11 if (labs(desired) > fabs(rc->curve_comp_error)) {
404 :     desired = (int)rc->curve_comp_error;
405 :     }
406 :     }
407 : suxen_drol 1.1.2.4
408 : edgomez 1.1.2.11 rc->curve_comp_error -= desired;
409 : suxen_drol 1.1.2.4
410 : edgomez 1.1.2.11 /* XXX: warning */
411 :     curve_temp = 0;
412 : suxen_drol 1.1.2.4
413 : edgomez 1.1.2.13 if ((rc->param.curve_compression_high + rc->param.curve_compression_low) && s->type != XVID_TYPE_IVOP) {
414 : suxen_drol 1.1.2.4
415 : edgomez 1.1.2.11 curve_temp = rc->curve_comp_scale;
416 :     if (dbytes > rc->avg_length[XVID_TYPE_PVOP-1]) {
417 :     curve_temp *= ((double)dbytes + (rc->avg_length[XVID_TYPE_PVOP-1] - dbytes) * rc->param.curve_compression_high / 100.0);
418 :     } else {
419 :     curve_temp *= ((double)dbytes + (rc->avg_length[XVID_TYPE_PVOP-1] - dbytes) * rc->param.curve_compression_low / 100.0);
420 :     }
421 : suxen_drol 1.1.2.4
422 : edgomez 1.1.2.11 /*
423 :     * End of code path for curve_temp, as told earlier, we are now
424 :     * obliged to scale the value to a bframe one using the inverse
425 :     * ratio applied earlier
426 :     */
427 :     if (s->type == XVID_TYPE_BVOP)
428 :     curve_temp *= rc->avg_length[XVID_TYPE_BVOP-1] / rc->avg_length[XVID_TYPE_PVOP-1];
429 : suxen_drol 1.1.2.4
430 : edgomez 1.1.2.11 desired += (int)curve_temp;
431 :     rc->curve_comp_error += curve_temp - (int)curve_temp;
432 :     } else {
433 :     /*
434 :     * End of code path for dbytes, as told earlier, we are now
435 :     * obliged to scale the value to a bframe one using the inverse
436 :     * ratio applied earlier
437 :     */
438 :     if (s->type == XVID_TYPE_BVOP)
439 :     dbytes *= rc->avg_length[XVID_TYPE_BVOP-1] / rc->avg_length[XVID_TYPE_PVOP-1];
440 :    
441 :     desired += (int)dbytes;
442 :     rc->curve_comp_error += dbytes - (int)dbytes;
443 : suxen_drol 1.1.2.4 }
444 :    
445 :    
446 : edgomez 1.1.2.11 /*
447 :     * We can't do bigger frames than first pass, this would be stupid as first
448 :     * pass is quant=2 and that reaching quant=1 is not worth it. We would lose
449 :     * many bytes and we would not not gain much quality.
450 :     */
451 :     if (desired > s->length) {
452 :     rc->curve_comp_error += desired - s->length;
453 :     desired = s->length;
454 :     } else {
455 :     if (desired < rc->min_length[s->type-1]) {
456 :     if (s->type == XVID_TYPE_IVOP){
457 :     rc->curve_comp_error -= rc->min_length[XVID_TYPE_IVOP-1] - desired;
458 :     }
459 :     desired = rc->min_length[s->type-1];
460 :     }
461 :     }
462 : suxen_drol 1.1.2.2
463 : edgomez 1.1.2.11 s->desired_length = desired;
464 : suxen_drol 1.1.2.2
465 : edgomez 1.1.2.15
466 :     /*
467 :     * if this keyframe is too close to the next, reduce it's byte allotment
468 :     * XXX: why do we do this after setting the desired length ?
469 :     */
470 : suxen_drol 1.1.2.2
471 : edgomez 1.1.2.11 if (s->type == XVID_TYPE_IVOP) {
472 :     int KFdistance = rc->keyframe_locations[rc->KF_idx] - rc->keyframe_locations[rc->KF_idx - 1];
473 : suxen_drol 1.1.2.2
474 : edgomez 1.1.2.11 if (KFdistance < rc->param.kftreshold) {
475 :    
476 :     KFdistance -= rc->param.min_key_interval;
477 : suxen_drol 1.1.2.2
478 : edgomez 1.1.2.11 if (KFdistance >= 0) {
479 :     int KF_min_size;
480 : suxen_drol 1.1.2.2
481 : edgomez 1.1.2.11 KF_min_size = desired * (100 - rc->param.kfreduction) / 100;
482 :     if (KF_min_size < 1)
483 :     KF_min_size = 1;
484 : suxen_drol 1.1.2.1
485 : edgomez 1.1.2.11 desired = KF_min_size + (desired - KF_min_size) * KFdistance /
486 :     (rc->param.kftreshold - rc->param.min_key_interval);
487 : suxen_drol 1.1.2.1
488 : edgomez 1.1.2.11 if (desired < 1)
489 :     desired = 1;
490 :     }
491 :     }
492 :     }
493 : suxen_drol 1.1.2.1
494 : edgomez 1.1.2.11 overflow = (int)((double)overflow * desired / rc->avg_length[XVID_TYPE_PVOP-1]);
495 : suxen_drol 1.1.2.1
496 : edgomez 1.1.2.11 /* Reign in overflow with huge frames */
497 : edgomez 1.1.2.13 if (labs(overflow) > labs(rc->overflow))
498 : edgomez 1.1.2.11 overflow = rc->overflow;
499 : suxen_drol 1.1.2.1
500 : edgomez 1.1.2.11 /* Make sure overflow doesn't run away */
501 :     if (overflow > desired * rc->param.max_overflow_improvement / 100) {
502 :     desired += (overflow <= desired) ? desired * rc->param.max_overflow_improvement / 100 :
503 :     overflow * rc->param.max_overflow_improvement / 100;
504 :     } else if (overflow < desired * rc->param.max_overflow_degradation / -100){
505 :     desired += desired * rc->param.max_overflow_degradation / -100;
506 :     } else {
507 :     desired += overflow;
508 :     }
509 : suxen_drol 1.1.2.1
510 : edgomez 1.1.2.11 /* Make sure we are not higher than desired frame size */
511 :     if (desired > rc->max_length) {
512 :     capped_to_max_framesize = 1;
513 :     desired = rc->max_length;
514 :     DPRINTF(XVID_DEBUG_RC,"[%i] Capped to maximum frame size\n",
515 :     data->frame_num);
516 :     }
517 : suxen_drol 1.1.2.1
518 : edgomez 1.1.2.11 /* Make sure to not scale below the minimum framesize */
519 :     if (desired < rc->min_length[s->type-1]) {
520 :     desired = rc->min_length[s->type-1];
521 :     DPRINTF(XVID_DEBUG_RC,"[%i] Capped to minimum frame size\n",
522 :     data->frame_num);
523 :     }
524 : suxen_drol 1.1.2.1
525 : edgomez 1.1.2.11 /*
526 :     * Don't laugh at this very 'simple' quant<->filesize relationship, it
527 :     * proves to be acurate enough for our algorithm
528 :     */
529 : edgomez 1.1.2.15 scaled_quant = (double)s->quant*(double)s->length/(double)desired;
530 :    
531 :     /*
532 :     * Quantizer has been scaled using floating point operations/results, we
533 :     * must cast it to integer
534 :     */
535 :     data->quant = (int)scaled_quant;
536 : suxen_drol 1.1.2.1
537 : edgomez 1.1.2.11 /* Let's clip the computed quantizer, if needed */
538 :     if (data->quant < 1) {
539 :     data->quant = 1;
540 :     } else if (data->quant > 31) {
541 :     data->quant = 31;
542 :     } else if (s->type != XVID_TYPE_IVOP) {
543 : suxen_drol 1.1.2.1
544 : edgomez 1.1.2.11 /*
545 : edgomez 1.1.2.15 * The frame quantizer has not been clipped, this appears to be a good
546 :     * computed quantizer, do not loose quantizer decimal part that we
547 :     * accumulate for later reuse when its sum represents a complete unit.
548 : edgomez 1.1.2.11 */
549 : edgomez 1.1.2.15 rc->quant_error[s->type-1][data->quant] += scaled_quant - (double)data->quant;
550 : suxen_drol 1.1.2.1
551 : edgomez 1.1.2.15 if (rc->quant_error[s->type-1][data->quant] >= 1.0) {
552 :     rc->quant_error[s->type-1][data->quant] -= 1.0;
553 :     data->quant++;
554 :     } else if (rc->quant_error[s->type-1][data->quant] <= -1.0) {
555 :     rc->quant_error[s->type-1][data->quant] += 1.0;
556 :     data->quant--;
557 : edgomez 1.1.2.11 }
558 : edgomez 1.1.2.15
559 : edgomez 1.1.2.11 }
560 : suxen_drol 1.1.2.1
561 : edgomez 1.1.2.11 /*
562 :     * Now we have a computed quant that is in the right quante range, with a
563 :     * possible +1 correction due to cumulated error. We can now safely clip
564 :     * the quantizer again with user's quant ranges. "Safely" means the Rate
565 :     * Control could learn more about this quantizer, this knowledge is useful
566 :     * for future frames even if it this quantizer won't be really used atm,
567 :     * that's why we don't perform this clipping earlier.
568 :     */
569 :     if (data->quant < data->min_quant[s->type-1]) {
570 :     data->quant = data->min_quant[s->type-1];
571 :     } else if (data->quant > data->max_quant[s->type-1]) {
572 :     data->quant = data->max_quant[s->type-1];
573 :     }
574 : suxen_drol 1.1.2.1
575 : edgomez 1.1.2.11 /*
576 :     * To avoid big quality jumps from frame to frame, we apply a "security"
577 :     * rule that makes |last_quant - new_quant| <= 2. This rule only applies
578 :     * to predicted frames (P and B)
579 :     */
580 :     if (s->type != XVID_TYPE_IVOP && rc->last_quant[s->type-1] && capped_to_max_framesize == 0) {
581 : suxen_drol 1.1.2.1
582 : edgomez 1.1.2.11 if (data->quant > rc->last_quant[s->type-1] + 2) {
583 :     data->quant = rc->last_quant[s->type-1] + 2;
584 :     DPRINTF(XVID_DEBUG_RC,
585 :     "[%i] p/b-frame quantizer prevented from rising too steeply\n",
586 :     data->frame_num);
587 :     }
588 :     if (data->quant < rc->last_quant[s->type-1] - 2) {
589 :     data->quant = rc->last_quant[s->type-1] - 2;
590 :     DPRINTF(XVID_DEBUG_RC,
591 :     "[%i] p/b-frame quantizer prevented from falling too steeply\n",
592 :     data->frame_num);
593 :     }
594 :     }
595 : suxen_drol 1.1.2.1
596 : edgomez 1.1.2.11 /*
597 :     * We don't want to pollute the RC history results when our computed quant
598 :     * has been computed from a capped frame size
599 :     */
600 : edgomez 1.1.2.13 if (capped_to_max_framesize == 0)
601 : edgomez 1.1.2.11 rc->last_quant[s->type-1] = data->quant;
602 : edgomez 1.1.2.17
603 :     /* Force frame type */
604 :     data->type = s->type;
605 : suxen_drol 1.1.2.1
606 : edgomez 1.1.2.11 return 0;
607 :     }
608 : suxen_drol 1.1.2.1
609 : edgomez 1.1.2.11 /*----------------------------------------------------------------------------
610 :     *--------------------------------------------------------------------------*/
611 : suxen_drol 1.1.2.1
612 : edgomez 1.1.2.11 static int
613 :     rc_2pass2_after(rc_2pass2_t * rc, xvid_plg_data_t * data)
614 :     {
615 : edgomez 1.1.2.12 const char frame_type[4] = { 'i', 'p', 'b', 's'};
616 :     stat_t * s = &rc->stats[data->frame_num];
617 : suxen_drol 1.1.2.1
618 : edgomez 1.1.2.11 /* Insufficent stats data */
619 :     if (data->frame_num >= rc->num_frames)
620 :     return 0;
621 : suxen_drol 1.1.2.1
622 : edgomez 1.1.2.11 rc->quant_count[data->quant]++;
623 : suxen_drol 1.1.2.1
624 : edgomez 1.1.2.11 if (data->type == XVID_TYPE_IVOP) {
625 :     int kfdiff = (rc->keyframe_locations[rc->KF_idx] - rc->keyframe_locations[rc->KF_idx - 1]);
626 : suxen_drol 1.1.2.1
627 : edgomez 1.1.2.11 rc->overflow += rc->KFoverflow;
628 :     rc->KFoverflow = s->desired_length - data->length;
629 :    
630 :     if (kfdiff > 1) { // non-consecutive keyframes
631 :     rc->KFoverflow_partial = rc->KFoverflow / (kfdiff - 1);
632 :     }else{ // consecutive keyframes
633 :     rc->overflow += rc->KFoverflow;
634 :     rc->KFoverflow = 0;
635 :     rc->KFoverflow_partial = 0;
636 :     }
637 :     rc->KF_idx++;
638 :     } else {
639 :     // distribute part of the keyframe overflow
640 :     rc->overflow += s->desired_length - data->length + rc->KFoverflow_partial;
641 :     rc->KFoverflow -= rc->KFoverflow_partial;
642 :     }
643 : suxen_drol 1.1.2.1
644 : 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",
645 : edgomez 1.1.2.11 data->frame_num,
646 : edgomez 1.1.2.12 frame_type[data->type-1],
647 : edgomez 1.1.2.11 data->quant,
648 :     s->length,
649 :     s->scaled_length,
650 :     data->length,
651 : edgomez 1.1.2.15 s->desired_length,
652 : edgomez 1.1.2.11 rc->overflow);
653 : suxen_drol 1.1.2.1
654 : edgomez 1.1.2.11 return(0);
655 : suxen_drol 1.1.2.1 }
656 :    
657 : edgomez 1.1.2.11 /*****************************************************************************
658 :     * Helper functions definition
659 :     ****************************************************************************/
660 : suxen_drol 1.1.2.1
661 : edgomez 1.1.2.11 #define BUF_SZ 1024
662 :     #define MAX_COLS 5
663 : suxen_drol 1.1.2.1
664 : edgomez 1.1.2.11 /* open stats file, and count num frames */
665 :     static int
666 :     det_stats_length(rc_2pass2_t * rc, char * filename)
667 : suxen_drol 1.1.2.1 {
668 : edgomez 1.1.2.11 FILE * f;
669 :     int n, ignore;
670 :     char type;
671 : suxen_drol 1.1.2.1
672 : edgomez 1.1.2.11 rc->num_frames = 0;
673 :     rc->num_keyframes = 0;
674 : suxen_drol 1.1.2.1
675 : edgomez 1.1.2.11 if ((f = fopen(filename, "rt")) == NULL)
676 :     return 0;
677 : suxen_drol 1.1.2.1
678 : edgomez 1.1.2.11 while((n = fscanf(f, "%c %d %d %d %d %d %d\n",
679 :     &type, &ignore, &ignore, &ignore, &ignore, &ignore, &ignore)) != EOF) {
680 :     if (type == 'i') {
681 :     rc->num_frames++;
682 :     rc->num_keyframes++;
683 :     }else if (type == 'p' || type == 'b' || type == 's') {
684 :     rc->num_frames++;
685 :     }
686 : suxen_drol 1.1.2.1 }
687 :    
688 : edgomez 1.1.2.11 fclose(f);
689 : suxen_drol 1.1.2.1
690 : edgomez 1.1.2.11 return 1;
691 :     }
692 : suxen_drol 1.1.2.2
693 : edgomez 1.1.2.11 /* open stats file(s) and read into rc->stats array */
694 : suxen_drol 1.1.2.4
695 : edgomez 1.1.2.11 static int
696 :     load_stats(rc_2pass2_t *rc, char * filename)
697 :     {
698 :     FILE * f;
699 :     int i, not_scaled;
700 :    
701 : suxen_drol 1.1.2.5
702 : edgomez 1.1.2.11 if ((f = fopen(filename, "rt"))==NULL)
703 :     return 0;
704 :    
705 :     i = 0;
706 :     not_scaled = 0;
707 :     while(i < rc->num_frames) {
708 :     stat_t * s = &rc->stats[i];
709 :     int n;
710 :     char type;
711 : suxen_drol 1.1.2.2
712 : edgomez 1.1.2.11 s->scaled_length = 0;
713 :     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);
714 :     if (n == EOF) break;
715 :     if (n < 7) {
716 :     not_scaled = 1;
717 :     }
718 : edgomez 1.1.2.6
719 : edgomez 1.1.2.11 if (type == 'i') {
720 :     s->type = XVID_TYPE_IVOP;
721 :     }else if (type == 'p' || type == 's') {
722 :     s->type = XVID_TYPE_PVOP;
723 :     }else if (type == 'b') {
724 :     s->type = XVID_TYPE_BVOP;
725 :     }else{ /* unknown type */
726 :     DPRINTF(XVID_DEBUG_RC, "unknown stats frame type; assuming pvop\n");
727 :     s->type = XVID_TYPE_PVOP;
728 : suxen_drol 1.1.2.4 }
729 :    
730 : edgomez 1.1.2.11 i++;
731 : suxen_drol 1.1.2.4 }
732 :    
733 : edgomez 1.1.2.11 rc->num_frames = i;
734 : suxen_drol 1.1.2.1
735 : edgomez 1.1.2.11 fclose(f);
736 : suxen_drol 1.1.2.1
737 : edgomez 1.1.2.11 return 1;
738 : suxen_drol 1.1.2.1 }
739 :    
740 : edgomez 1.1.2.11 #if 0
741 :     static void print_stats(rc_2pass2_t * rc)
742 :     {
743 :     int i;
744 :     DPRINTF(XVID_DEBUG_RC, "type quant length scaled_length\n");
745 :     for (i = 0; i < rc->num_frames; i++) {
746 :     stat_t * s = &rc->stats[i];
747 :     DPRINTF(XVID_DEBUG_RC, "%d %d %d %d\n", s->type, s->quant, s->length, s->scaled_length);
748 :     }
749 :     }
750 :     #endif
751 : suxen_drol 1.1.2.1
752 : edgomez 1.1.2.11 /* pre-process the statistics data
753 :     - for each type, count, tot_length, min_length, max_length
754 :     - set keyframes_locations
755 :     */
756 : suxen_drol 1.1.2.1
757 : edgomez 1.1.2.11 static void
758 :     pre_process0(rc_2pass2_t * rc)
759 : suxen_drol 1.1.2.1 {
760 : edgomez 1.1.2.11 int i,j;
761 : edgomez 1.1.2.7
762 : edgomez 1.1.2.15 /*
763 :     * *rc fields initialization
764 :     * NB: INT_MAX and INT_MIN are used in order to be immediately replaced
765 :     * with real values of the 1pass
766 :     */
767 :     for (i=0; i<3; i++) {
768 :     rc->count[i]=0;
769 :     rc->tot_length[i] = 0;
770 : edgomez 1.1.2.11 rc->min_length[i] = INT_MAX;
771 :     }
772 : edgomez 1.1.2.7
773 : edgomez 1.1.2.11 rc->max_length = INT_MIN;
774 : edgomez 1.1.2.7
775 : edgomez 1.1.2.15 /*
776 :     * Loop through all frames and find/compute all the stuff this function
777 :     * is supposed to do
778 :     */
779 :     for (i=j=0; i<rc->num_frames; i++) {
780 :     stat_t * s = &rc->stats[i];
781 : suxen_drol 1.1.2.1
782 : edgomez 1.1.2.15 rc->count[s->type-1]++;
783 :     rc->tot_length[s->type-1] += s->length;
784 : suxen_drol 1.1.2.1
785 : edgomez 1.1.2.15 if (s->length < rc->min_length[s->type-1]) {
786 :     rc->min_length[s->type-1] = s->length;
787 :     }
788 : suxen_drol 1.1.2.1
789 : edgomez 1.1.2.15 if (s->length > rc->max_length) {
790 :     rc->max_length = s->length;
791 :     }
792 :    
793 :     if (s->type == XVID_TYPE_IVOP) {
794 :     rc->keyframe_locations[j] = i;
795 :     j++;
796 :     }
797 :     }
798 : edgomez 1.1.2.7
799 :     /*
800 : edgomez 1.1.2.11 * Nota Bene:
801 :     * The "per sequence" overflow system considers a natural sequence to be
802 :     * formed by all frames between two iframes, so if we want to make sure
803 :     * the system does not go nuts during last sequence, we force the last
804 :     * frame to appear in the keyframe locations array.
805 : edgomez 1.1.2.7 */
806 : edgomez 1.1.2.11 rc->keyframe_locations[j] = i;
807 : edgomez 1.1.2.7
808 : edgomez 1.1.2.11 DPRINTF(XVID_DEBUG_RC, "Min 1st pass IFrame length: %d\n", rc->min_length[0]);
809 :     DPRINTF(XVID_DEBUG_RC, "Min 1st pass PFrame length: %d\n", rc->min_length[1]);
810 :     DPRINTF(XVID_DEBUG_RC, "Min 1st pass BFrame length: %d\n", rc->min_length[2]);
811 :     }
812 :    
813 :     /* calculate zone weight "center" */
814 : edgomez 1.1.2.7
815 : edgomez 1.1.2.11 static void
816 :     zone_process(rc_2pass2_t *rc, const xvid_plg_create_t * create)
817 :     {
818 :     int i,j;
819 :     int n = 0;
820 : suxen_drol 1.1.2.5
821 : edgomez 1.1.2.11 rc->avg_weight = 0.0;
822 :     rc->tot_quant = 0;
823 : suxen_drol 1.1.2.1
824 :    
825 : edgomez 1.1.2.11 if (create->num_zones == 0) {
826 :     for (j = 0; j < rc->num_frames; j++) {
827 :     rc->stats[j].zone_mode = XVID_ZONE_WEIGHT;
828 :     rc->stats[j].weight = 1.0;
829 :     }
830 :     rc->avg_weight += rc->num_frames * 1.0;
831 :     n += rc->num_frames;
832 :     }
833 :    
834 :    
835 :     for(i=0; i < create->num_zones; i++) {
836 :    
837 :     int next = (i+1<create->num_zones) ? create->zones[i+1].frame : rc->num_frames;
838 :    
839 :     if (i==0 && create->zones[i].frame > 0) {
840 :     for (j = 0; j < create->zones[i].frame && j < rc->num_frames; j++) {
841 :     rc->stats[j].zone_mode = XVID_ZONE_WEIGHT;
842 :     rc->stats[j].weight = 1.0;
843 :     }
844 :     rc->avg_weight += create->zones[i].frame * 1.0;
845 :     n += create->zones[i].frame;
846 :     }
847 :    
848 :     if (create->zones[i].mode == XVID_ZONE_WEIGHT) {
849 :     for (j = create->zones[i].frame; j < next && j < rc->num_frames; j++ ) {
850 :     rc->stats[j].zone_mode = XVID_ZONE_WEIGHT;
851 :     rc->stats[j].weight = (double)create->zones[i].increment / (double)create->zones[i].base;
852 :     }
853 :     next -= create->zones[i].frame;
854 :     rc->avg_weight += (double)(next * create->zones[i].increment) / (double)create->zones[i].base;
855 :     n += next;
856 :     }else{ // XVID_ZONE_QUANT
857 :     for (j = create->zones[i].frame; j < next && j < rc->num_frames; j++ ) {
858 :     rc->stats[j].zone_mode = XVID_ZONE_QUANT;
859 :     rc->stats[j].weight = (double)create->zones[i].increment / (double)create->zones[i].base;
860 :     rc->tot_quant += rc->stats[j].length;
861 :     }
862 :     }
863 :     }
864 :     rc->avg_weight = n>0 ? rc->avg_weight/n : 1.0;
865 : suxen_drol 1.1.2.1
866 : 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);
867 :     }
868 : suxen_drol 1.1.2.1
869 :    
870 : edgomez 1.1.2.11 /* scale the curve */
871 : suxen_drol 1.1.2.1
872 : edgomez 1.1.2.11 static void
873 :     internal_scale(rc_2pass2_t *rc)
874 :     {
875 :     int64_t target = rc->target - rc->tot_quant;
876 :     int64_t pass1_length = rc->tot_length[0] + rc->tot_length[1] + rc->tot_length[2] - rc->tot_quant;
877 :     double scaler;
878 : edgomez 1.1.2.14 int i, num_MBs;
879 : suxen_drol 1.1.2.1
880 : edgomez 1.1.2.11 /* Let's compute a linear scaler in order to perform curve scaling */
881 :     scaler = (double)target / (double)pass1_length;
882 : suxen_drol 1.1.2.1
883 : edgomez 1.1.2.11 if (target <= 0 || pass1_length <= 0 || target >= pass1_length) {
884 :     DPRINTF(XVID_DEBUG_RC, "WARNING: Undersize detected\n");
885 :     scaler = 1.0;
886 :     }
887 : edgomez 1.1.2.7
888 : edgomez 1.1.2.11 DPRINTF(XVID_DEBUG_RC,
889 :     "Before correction: target=%i, tot_length=%i, scaler=%f\n",
890 :     (int)target, (int)pass1_length, scaler);
891 : edgomez 1.1.2.7
892 : edgomez 1.1.2.11 /*
893 : edgomez 1.1.2.14 * Compute min frame lengths (for each frame type) according to the number
894 :     * of MBs. We sum all blocks count from frame 0 (should be an IFrame, so
895 :     * blocks[0] should be enough) to know how many MBs there are.
896 : edgomez 1.1.2.16 *
897 :     * We compare these hardcoded values with observed values in first pass
898 :     * (determined in pre_process0).Then we keep the real minimum.
899 : edgomez 1.1.2.14 */
900 :     num_MBs = rc->stats[0].blks[0] + rc->stats[0].blks[1] + rc->stats[0].blks[2];
901 : edgomez 1.1.2.16
902 :     if(rc->min_length[0] > ((num_MBs*22) + 240) / 8)
903 :     rc->min_length[0] = ((num_MBs*22) + 240) / 8;
904 :    
905 :     if(rc->min_length[1] > ((num_MBs) + 88) / 8)
906 :     rc->min_length[1] = ((num_MBs) + 88) / 8;
907 :    
908 :     if(rc->min_length[2] > 8)
909 :     rc->min_length[2] = 8;
910 : edgomez 1.1.2.14
911 :     /*
912 : edgomez 1.1.2.11 * Perform an initial scale pass.
913 :     * If a frame size is scaled underneath our hardcoded minimums, then we
914 :     * force the frame size to the minimum, and deduct the original & scaled
915 :     * frame length from the original and target total lengths
916 :     */
917 :     for (i=0; i<rc->num_frames; i++) {
918 :     stat_t * s = &rc->stats[i];
919 :     int len;
920 : suxen_drol 1.1.2.1
921 : edgomez 1.1.2.11 if (s->zone_mode == XVID_ZONE_QUANT) {
922 :     s->scaled_length = s->length;
923 :     continue;
924 : edgomez 1.1.2.7 }
925 : suxen_drol 1.1.2.1
926 : edgomez 1.1.2.15 /* Compute the scaled length */
927 : edgomez 1.1.2.11 len = (int)((double)s->length * scaler * s->weight / rc->avg_weight);
928 : edgomez 1.1.2.7
929 : edgomez 1.1.2.11 /* Compare with the computed minimum */
930 : edgomez 1.1.2.16 if (len < rc->min_length[s->type-1]) {
931 : edgomez 1.1.2.11 /* force frame size to our computed minimum */
932 : edgomez 1.1.2.16 s->scaled_length = rc->min_length[s->type-1];
933 : edgomez 1.1.2.11 target -= s->scaled_length;
934 :     pass1_length -= s->length;
935 :     } else {
936 :     /* Do nothing for now, we'll scale this later */
937 :     s->scaled_length = 0;
938 : edgomez 1.1.2.7 }
939 :     }
940 : suxen_drol 1.1.2.1
941 : edgomez 1.1.2.11 /* Correct the scaler for all non forced frames */
942 :     scaler = (double)target / (double)pass1_length;
943 : suxen_drol 1.1.2.5
944 : edgomez 1.1.2.11 /* Detect undersizing */
945 :     if (target <= 0 || pass1_length <= 0 || target >= pass1_length) {
946 :     DPRINTF(XVID_DEBUG_RC, "WARNING: Undersize detected\n");
947 :     scaler = 1.0;
948 : edgomez 1.1.2.7 }
949 : suxen_drol 1.1.2.1
950 : edgomez 1.1.2.11 DPRINTF(XVID_DEBUG_RC,
951 :     "After correction: target=%i, tot_length=%i, scaler=%f\n",
952 :     (int)target, (int)pass1_length, scaler);
953 : suxen_drol 1.1.2.4
954 : edgomez 1.1.2.11 /* Do another pass with the new scaler */
955 :     for (i=0; i<rc->num_frames; i++) {
956 :     stat_t * s = &rc->stats[i];
957 : suxen_drol 1.1.2.4
958 : edgomez 1.1.2.11 /* Ignore frame with forced frame sizes */
959 :     if (s->scaled_length == 0)
960 :     s->scaled_length = (int)((double)s->length * scaler * s->weight / rc->avg_weight);
961 :     }
962 :     }
963 : suxen_drol 1.1.2.1
964 : edgomez 1.1.2.11 static void
965 :     pre_process1(rc_2pass2_t * rc)
966 :     {
967 :     int i;
968 :     double total1, total2;
969 :     uint64_t ivop_boost_total;
970 : suxen_drol 1.1.2.1
971 : edgomez 1.1.2.11 ivop_boost_total = 0;
972 :     rc->curve_comp_error = 0;
973 : suxen_drol 1.1.2.1
974 : edgomez 1.1.2.11 for (i=0; i<3; i++) {
975 :     rc->tot_scaled_length[i] = 0;
976 :     }
977 : suxen_drol 1.1.2.1
978 : edgomez 1.1.2.11 for (i=0; i<rc->num_frames; i++) {
979 :     stat_t * s = &rc->stats[i];
980 : suxen_drol 1.1.2.1
981 : edgomez 1.1.2.11 rc->tot_scaled_length[s->type-1] += s->scaled_length;
982 :    
983 :     if (s->type == XVID_TYPE_IVOP) {
984 :     ivop_boost_total += s->scaled_length * rc->param.keyframe_boost / 100;
985 :     }
986 :     }
987 : suxen_drol 1.1.2.1
988 : 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) /
989 :     (rc->tot_scaled_length[XVID_TYPE_PVOP-1] + rc->tot_scaled_length[XVID_TYPE_BVOP-1]));
990 : edgomez 1.1.2.7
991 : edgomez 1.1.2.11 for(i=0; i<3; i++) {
992 :     if (rc->count[i] == 0 || rc->movie_curve == 0) {
993 :     rc->avg_length[i] = 1;
994 :     }else{
995 :     rc->avg_length[i] = rc->tot_scaled_length[i] / rc->count[i] / rc->movie_curve;
996 :     }
997 :     }
998 : edgomez 1.1.2.7
999 : edgomez 1.1.2.11 /* --- */
1000 : edgomez 1.1.2.7
1001 : edgomez 1.1.2.11 total1=total2=0;
1002 : edgomez 1.1.2.7
1003 : edgomez 1.1.2.11 for (i=0; i<rc->num_frames; i++) {
1004 :     stat_t * s = &rc->stats[i];
1005 : edgomez 1.1.2.7
1006 : edgomez 1.1.2.11 if (s->type != XVID_TYPE_IVOP) {
1007 :     double dbytes,dbytes2;
1008 : suxen_drol 1.1.2.1
1009 : edgomez 1.1.2.11 dbytes = s->scaled_length / rc->movie_curve;
1010 :     dbytes2 = 0; /* XXX: warning */
1011 :     total1 += dbytes;
1012 :     if (s->type == XVID_TYPE_BVOP)
1013 :     dbytes *= rc->avg_length[XVID_TYPE_PVOP-1] / rc->avg_length[XVID_TYPE_BVOP-1];
1014 : suxen_drol 1.1.2.1
1015 : edgomez 1.1.2.13 if (dbytes > rc->avg_length[XVID_TYPE_PVOP-1]) {
1016 :     dbytes2=((double)dbytes + (rc->avg_length[XVID_TYPE_PVOP-1] - dbytes) * rc->param.curve_compression_high / 100.0);
1017 :     } else {
1018 :     dbytes2 = ((double)dbytes + (rc->avg_length[XVID_TYPE_PVOP-1] - dbytes) * rc->param.curve_compression_low / 100.0);
1019 :     }
1020 : suxen_drol 1.1.2.1
1021 : edgomez 1.1.2.11 if (s->type == XVID_TYPE_BVOP) {
1022 :     dbytes2 *= rc->avg_length[XVID_TYPE_BVOP-1] / rc->avg_length[XVID_TYPE_PVOP-1];
1023 :     if (dbytes2 < rc->min_length[XVID_TYPE_BVOP-1])
1024 :     dbytes2 = rc->min_length[XVID_TYPE_BVOP-1];
1025 :     }else{
1026 :     if (dbytes2 < rc->min_length[XVID_TYPE_PVOP-1])
1027 :     dbytes2 = rc->min_length[XVID_TYPE_PVOP-1];
1028 :     }
1029 :     total2 += dbytes2;
1030 : suxen_drol 1.1.2.1 }
1031 :     }
1032 :    
1033 : edgomez 1.1.2.11 rc->curve_comp_scale = total1 / total2;
1034 : suxen_drol 1.1.2.1
1035 : edgomez 1.1.2.13 DPRINTF(XVID_DEBUG_RC, "middle frame size for asymmetric curve compression: %i\n",
1036 : edgomez 1.1.2.11 (int)(rc->avg_length[XVID_TYPE_PVOP-1] * rc->curve_comp_scale));
1037 : suxen_drol 1.1.2.1
1038 : edgomez 1.1.2.11 rc->overflow = 0;
1039 :     rc->KFoverflow = 0;
1040 :     rc->KFoverflow_partial = 0;
1041 :     rc->KF_idx = 1;
1042 : suxen_drol 1.1.2.1 }

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