Parent Directory | Revision Log
Revision 1047 - (view) (download)
1 : | suxen_drol | 942 | /****************************************************************************** |
2 : | * | ||
3 : | * XviD Bit Rate Controller Library | ||
4 : | edgomez | 1042 | * - VBR 2 pass bitrate controller implementation - |
5 : | suxen_drol | 942 | * |
6 : | edgomez | 1037 | * 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 | 942 | * |
11 : | edgomez | 1037 | * 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 | 942 | * |
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 | 1047 | * $Id: plugin_2pass2.c,v 1.1.2.14 2003-05-29 10:36:41 edgomez Exp $ |
29 : | suxen_drol | 942 | * |
30 : | *****************************************************************************/ | ||
31 : | |||
32 : | #include <stdio.h> | ||
33 : | #include <math.h> | ||
34 : | edgomez | 1040 | #include <limits.h> |
35 : | suxen_drol | 942 | |
36 : | edgomez | 1042 | #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 : | suxen_drol | 942 | |
51 : | edgomez | 1042 | /* 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 : | |||
71 : | /* Statistics */ | ||
72 : | suxen_drol | 942 | typedef struct { |
73 : | int type; /* first pass type */ | ||
74 : | int quant; /* first pass quant */ | ||
75 : | suxen_drol | 977 | int blks[3]; /* k,m,y blks */ |
76 : | suxen_drol | 942 | int length; /* first pass length */ |
77 : | suxen_drol | 1030 | 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 | 942 | } stat_t; |
83 : | |||
84 : | edgomez | 1042 | /* Context struct */ |
85 : | suxen_drol | 942 | typedef struct |
86 : | { | ||
87 : | xvid_plugin_2pass2_t param; | ||
88 : | |||
89 : | /* constant statistical data */ | ||
90 : | suxen_drol | 977 | int num_frames; |
91 : | suxen_drol | 942 | int num_keyframes; |
92 : | suxen_drol | 1030 | uint64_t target; /* target filesize */ |
93 : | suxen_drol | 977 | |
94 : | suxen_drol | 942 | 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 | 1030 | |
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 | 942 | 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 | 1030 | |
125 : | double fq_error; | ||
126 : | suxen_drol | 942 | } rc_2pass2_t; |
127 : | |||
128 : | |||
129 : | edgomez | 1042 | /***************************************************************************** |
130 : | * Sub plugin functions prototypes | ||
131 : | ****************************************************************************/ | ||
132 : | suxen_drol | 942 | |
133 : | edgomez | 1042 | 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 : | suxen_drol | 942 | |
138 : | edgomez | 1042 | /***************************************************************************** |
139 : | * Plugin definition | ||
140 : | ****************************************************************************/ | ||
141 : | suxen_drol | 942 | |
142 : | edgomez | 1042 | int |
143 : | xvid_plugin_2pass2(void * handle, int opt, void * param1, void * param2) | ||
144 : | suxen_drol | 942 | { |
145 : | edgomez | 1042 | switch(opt) { |
146 : | case XVID_PLG_INFO : | ||
147 : | suxen_drol | 942 | return 0; |
148 : | |||
149 : | edgomez | 1042 | case XVID_PLG_CREATE : |
150 : | return rc_2pass2_create((xvid_plg_create_t*)param1, param2); | ||
151 : | suxen_drol | 942 | |
152 : | edgomez | 1042 | case XVID_PLG_DESTROY : |
153 : | return rc_2pass2_destroy((rc_2pass2_t*)handle, (xvid_plg_destroy_t*)param1); | ||
154 : | suxen_drol | 942 | |
155 : | edgomez | 1042 | case XVID_PLG_BEFORE : |
156 : | return rc_2pass2_before((rc_2pass2_t*)handle, (xvid_plg_data_t*)param1); | ||
157 : | suxen_drol | 942 | |
158 : | edgomez | 1042 | case XVID_PLG_AFTER : |
159 : | return rc_2pass2_after((rc_2pass2_t*)handle, (xvid_plg_data_t*)param1); | ||
160 : | suxen_drol | 977 | } |
161 : | edgomez | 1037 | |
162 : | edgomez | 1042 | return XVID_ERR_FAIL; |
163 : | suxen_drol | 977 | } |
164 : | |||
165 : | edgomez | 1042 | /***************************************************************************** |
166 : | * Sub plugin functions definitions | ||
167 : | ****************************************************************************/ | ||
168 : | suxen_drol | 977 | |
169 : | edgomez | 1042 | /* 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 | 977 | |
177 : | edgomez | 1042 | /*---------------------------------------------------------------------------- |
178 : | *--------------------------------------------------------------------------*/ | ||
179 : | suxen_drol | 942 | |
180 : | edgomez | 1042 | static int |
181 : | rc_2pass2_create(xvid_plg_create_t * create, rc_2pass2_t **handle) | ||
182 : | suxen_drol | 942 | { |
183 : | xvid_plugin_2pass2_t * param = (xvid_plugin_2pass2_t *)create->param; | ||
184 : | rc_2pass2_t * rc; | ||
185 : | suxen_drol | 1030 | int i; |
186 : | suxen_drol | 942 | |
187 : | rc = malloc(sizeof(rc_2pass2_t)); | ||
188 : | if (rc == NULL) | ||
189 : | return XVID_ERR_MEMORY; | ||
190 : | |||
191 : | rc->param = *param; | ||
192 : | |||
193 : | edgomez | 1042 | #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 : | suxen_drol | 942 | |
203 : | edgomez | 1042 | /* 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 | 942 | |
209 : | edgomez | 1042 | /* Count frames in the stats file */ |
210 : | edgomez | 1046 | if (!det_stats_length(rc, param->filename)) { |
211 : | suxen_drol | 1032 | DPRINTF(XVID_DEBUG_RC,"fopen %s failed\n", param->filename); |
212 : | suxen_drol | 942 | free(rc); |
213 : | return XVID_ERR_FAIL; | ||
214 : | } | ||
215 : | |||
216 : | edgomez | 1042 | /* Allocate the stats' memory */ |
217 : | if ((rc->stats = malloc(rc->num_frames * sizeof(stat_t))) == NULL) { | ||
218 : | suxen_drol | 942 | free(rc); |
219 : | return XVID_ERR_MEMORY; | ||
220 : | } | ||
221 : | |||
222 : | edgomez | 1037 | /* |
223 : | edgomez | 1042 | * Allocate keyframes location's memory |
224 : | * PS: see comment in pre_process0 for the +1 location requirement | ||
225 : | edgomez | 1037 | */ |
226 : | suxen_drol | 942 | 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 : | |||
232 : | suxen_drol | 977 | if (!load_stats(rc, param->filename)) { |
233 : | suxen_drol | 1032 | DPRINTF(XVID_DEBUG_RC,"fopen %s failed\n", param->filename); |
234 : | suxen_drol | 942 | free(rc->keyframe_locations); |
235 : | free(rc->stats); | ||
236 : | free(rc); | ||
237 : | return XVID_ERR_FAIL; | ||
238 : | } | ||
239 : | |||
240 : | /* pre-process our stats */ | ||
241 : | suxen_drol | 977 | |
242 : | suxen_drol | 1030 | if (rc->num_frames < create->fbase/create->fincr) { |
243 : | rc->target = rc->param.bitrate / 8; /* one second */ | ||
244 : | edgomez | 1042 | } else { |
245 : | edgomez | 1040 | rc->target = |
246 : | ((uint64_t)rc->param.bitrate * (uint64_t)rc->num_frames * (uint64_t)create->fincr) / \ | ||
247 : | ((uint64_t)create->fbase * 8); | ||
248 : | suxen_drol | 977 | } |
249 : | suxen_drol | 1030 | |
250 : | edgomez | 1040 | 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 | 1032 | |
255 : | edgomez | 1041 | /* 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 | 977 | |
260 : | pre_process0(rc); | ||
261 : | edgomez | 1036 | |
262 : | suxen_drol | 977 | if (rc->param.bitrate) { |
263 : | suxen_drol | 1030 | zone_process(rc, create); |
264 : | suxen_drol | 977 | internal_scale(rc); |
265 : | suxen_drol | 1030 | }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 : | } | ||
274 : | pre_process1(rc); | ||
275 : | |||
276 : | 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 : | |||
282 : | rc->fq_error = 0; | ||
283 : | suxen_drol | 977 | |
284 : | suxen_drol | 942 | *handle = rc; |
285 : | return(0); | ||
286 : | } | ||
287 : | |||
288 : | edgomez | 1042 | /*---------------------------------------------------------------------------- |
289 : | *--------------------------------------------------------------------------*/ | ||
290 : | suxen_drol | 942 | |
291 : | edgomez | 1042 | static int |
292 : | rc_2pass2_destroy(rc_2pass2_t * rc, xvid_plg_destroy_t * destroy) | ||
293 : | suxen_drol | 942 | { |
294 : | free(rc->keyframe_locations); | ||
295 : | free(rc->stats); | ||
296 : | free(rc); | ||
297 : | return(0); | ||
298 : | } | ||
299 : | |||
300 : | edgomez | 1042 | /*---------------------------------------------------------------------------- |
301 : | *--------------------------------------------------------------------------*/ | ||
302 : | suxen_drol | 942 | |
303 : | edgomez | 1042 | static int |
304 : | rc_2pass2_before(rc_2pass2_t * rc, xvid_plg_data_t * data) | ||
305 : | suxen_drol | 942 | { |
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 : | |||
313 : | edgomez | 1037 | /* |
314 : | * 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 : | */ | ||
317 : | suxen_drol | 942 | |
318 : | edgomez | 1037 | /* First case: Another plugin has already set a quantizer */ |
319 : | if (data->quant > 0) | ||
320 : | return(0); | ||
321 : | suxen_drol | 942 | |
322 : | edgomez | 1037 | /* Second case: We are in a Quant zone */ |
323 : | if (s->zone_mode == XVID_ZONE_QUANT) { | ||
324 : | suxen_drol | 942 | |
325 : | edgomez | 1037 | 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 | 1032 | |
331 : | edgomez | 1037 | return(0); |
332 : | suxen_drol | 942 | |
333 : | edgomez | 1037 | } |
334 : | suxen_drol | 977 | |
335 : | edgomez | 1037 | /* Third case: insufficent stats data */ |
336 : | if (data->frame_num >= rc->num_frames) | ||
337 : | return 0; | ||
338 : | suxen_drol | 942 | |
339 : | edgomez | 1037 | /* |
340 : | * The last case is the one every normal minded developer should fear to | ||
341 : | * maintain in a project :-) | ||
342 : | */ | ||
343 : | suxen_drol | 942 | |
344 : | edgomez | 1037 | /* XXX: why by 8 */ |
345 : | overflow = rc->overflow / 8; | ||
346 : | suxen_drol | 942 | |
347 : | edgomez | 1037 | /* |
348 : | * The rc->overflow field represents the overflow in current scene (between two | ||
349 : | edgomez | 1042 | * IFrames) so we must not forget to reset it if we are entering a new scene |
350 : | edgomez | 1037 | */ |
351 : | edgomez | 1046 | if (s->type == XVID_TYPE_IVOP) |
352 : | edgomez | 1037 | overflow = 0; |
353 : | suxen_drol | 942 | |
354 : | edgomez | 1037 | desired = s->scaled_length; |
355 : | suxen_drol | 942 | |
356 : | edgomez | 1037 | dbytes = desired; |
357 : | edgomez | 1046 | if (s->type == XVID_TYPE_IVOP) |
358 : | edgomez | 1037 | dbytes += desired * rc->param.keyframe_boost / 100; |
359 : | dbytes /= rc->movie_curve; | ||
360 : | suxen_drol | 942 | |
361 : | edgomez | 1037 | /* |
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 : | edgomez | 1042 | if (s->type == XVID_TYPE_BVOP) |
370 : | edgomez | 1037 | dbytes *= rc->avg_length[XVID_TYPE_PVOP-1] / rc->avg_length[XVID_TYPE_BVOP-1]; |
371 : | suxen_drol | 942 | |
372 : | edgomez | 1037 | /* |
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 : | edgomez | 1042 | } else { |
379 : | edgomez | 1037 | desired = (int)(rc->curve_comp_error * dbytes / |
380 : | rc->avg_length[XVID_TYPE_PVOP-1] / rc->param.bitrate_payback_delay); | ||
381 : | suxen_drol | 942 | |
382 : | edgomez | 1037 | if (labs(desired) > fabs(rc->curve_comp_error)) { |
383 : | desired = (int)rc->curve_comp_error; | ||
384 : | } | ||
385 : | } | ||
386 : | suxen_drol | 942 | |
387 : | edgomez | 1037 | rc->curve_comp_error -= desired; |
388 : | suxen_drol | 942 | |
389 : | edgomez | 1037 | /* |
390 : | * 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 : | */ | ||
394 : | suxen_drol | 942 | |
395 : | edgomez | 1037 | /* XXX: warning */ |
396 : | curve_temp = 0; | ||
397 : | suxen_drol | 942 | |
398 : | edgomez | 1046 | if ((rc->param.curve_compression_high + rc->param.curve_compression_low) && s->type != XVID_TYPE_IVOP) { |
399 : | suxen_drol | 942 | |
400 : | edgomez | 1037 | 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 | 942 | |
407 : | edgomez | 1037 | /* |
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 | 1032 | |
415 : | edgomez | 1037 | 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 : | edgomez | 1042 | if (s->type == XVID_TYPE_BVOP) |
424 : | edgomez | 1037 | dbytes *= rc->avg_length[XVID_TYPE_BVOP-1] / rc->avg_length[XVID_TYPE_PVOP-1]; |
425 : | edgomez | 1042 | |
426 : | edgomez | 1037 | desired += (int)dbytes; |
427 : | rc->curve_comp_error += dbytes - (int)dbytes; | ||
428 : | } | ||
429 : | suxen_drol | 942 | |
430 : | |||
431 : | edgomez | 1037 | /* |
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 : | edgomez | 1042 | } else { |
440 : | edgomez | 1037 | 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 | 942 | |
448 : | edgomez | 1037 | s->desired_length = desired; |
449 : | suxen_drol | 942 | |
450 : | edgomez | 1037 | |
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 | 942 | |
454 : | edgomez | 1037 | 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 | 942 | |
457 : | edgomez | 1037 | if (KFdistance < rc->param.kftreshold) { |
458 : | |||
459 : | edgomez | 1042 | KFdistance -= rc->param.min_key_interval; |
460 : | suxen_drol | 942 | |
461 : | edgomez | 1037 | if (KFdistance >= 0) { |
462 : | int KF_min_size; | ||
463 : | suxen_drol | 942 | |
464 : | edgomez | 1037 | KF_min_size = desired * (100 - rc->param.kfreduction) / 100; |
465 : | if (KF_min_size < 1) | ||
466 : | KF_min_size = 1; | ||
467 : | suxen_drol | 942 | |
468 : | edgomez | 1037 | desired = KF_min_size + (desired - KF_min_size) * KFdistance / |
469 : | (rc->param.kftreshold - rc->param.min_key_interval); | ||
470 : | suxen_drol | 942 | |
471 : | edgomez | 1037 | if (desired < 1) |
472 : | desired = 1; | ||
473 : | } | ||
474 : | } | ||
475 : | } | ||
476 : | suxen_drol | 942 | |
477 : | edgomez | 1037 | overflow = (int)((double)overflow * desired / rc->avg_length[XVID_TYPE_PVOP-1]); |
478 : | suxen_drol | 942 | |
479 : | edgomez | 1037 | /* Reign in overflow with huge frames */ |
480 : | edgomez | 1046 | if (labs(overflow) > labs(rc->overflow)) |
481 : | edgomez | 1037 | overflow = rc->overflow; |
482 : | suxen_drol | 942 | |
483 : | edgomez | 1037 | /* 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 | 942 | |
493 : | edgomez | 1037 | /* 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 : | edgomez | 1040 | DPRINTF(XVID_DEBUG_RC,"[%i] Capped to maximum frame size\n", |
498 : | data->frame_num); | ||
499 : | edgomez | 1037 | } |
500 : | suxen_drol | 942 | |
501 : | edgomez | 1037 | /* Make sure to not scale below the minimum framesize */ |
502 : | edgomez | 1040 | if (desired < rc->min_length[s->type-1]) { |
503 : | edgomez | 1037 | desired = rc->min_length[s->type-1]; |
504 : | edgomez | 1040 | DPRINTF(XVID_DEBUG_RC,"[%i] Capped to minimum frame size\n", |
505 : | data->frame_num); | ||
506 : | } | ||
507 : | suxen_drol | 942 | |
508 : | edgomez | 1037 | /* |
509 : | * Don't laugh at this very 'simple' quant<->filesize relationship, it | ||
510 : | * proves to be acurate enough for our algorithm | ||
511 : | */ | ||
512 : | edgomez | 1042 | data->quant = s->quant*s->length/desired; |
513 : | suxen_drol | 942 | |
514 : | edgomez | 1037 | /* 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 | 942 | |
521 : | edgomez | 1037 | /* |
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 : | edgomez | 1042 | if (s->type == XVID_TYPE_BVOP) { |
529 : | edgomez | 1037 | rc->bquant_error[data->quant] += ((double)(s->quant * s->length) / desired) - data->quant; |
530 : | suxen_drol | 942 | |
531 : | edgomez | 1037 | 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 | 942 | |
538 : | edgomez | 1037 | if (rc->pquant_error[data->quant] >= 1.0) { |
539 : | rc->pquant_error[data->quant] -= 1.0; | ||
540 : | edgomez | 1042 | data->quant++; |
541 : | edgomez | 1037 | } |
542 : | } | ||
543 : | } | ||
544 : | suxen_drol | 942 | |
545 : | edgomez | 1037 | /* |
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 | 942 | |
559 : | edgomez | 1037 | /* |
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 | 942 | |
566 : | edgomez | 1037 | if (data->quant > rc->last_quant[s->type-1] + 2) { |
567 : | data->quant = rc->last_quant[s->type-1] + 2; | ||
568 : | edgomez | 1040 | DPRINTF(XVID_DEBUG_RC, |
569 : | "[%i] p/b-frame quantizer prevented from rising too steeply\n", | ||
570 : | data->frame_num); | ||
571 : | edgomez | 1037 | } |
572 : | if (data->quant < rc->last_quant[s->type-1] - 2) { | ||
573 : | data->quant = rc->last_quant[s->type-1] - 2; | ||
574 : | edgomez | 1040 | DPRINTF(XVID_DEBUG_RC, |
575 : | "[%i] p/b-frame quantizer prevented from falling too steeply\n", | ||
576 : | data->frame_num); | ||
577 : | edgomez | 1037 | } |
578 : | } | ||
579 : | suxen_drol | 1030 | |
580 : | edgomez | 1037 | /* |
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 | 1046 | if (capped_to_max_framesize == 0) |
585 : | edgomez | 1037 | rc->last_quant[s->type-1] = data->quant; |
586 : | suxen_drol | 1030 | |
587 : | edgomez | 1037 | return 0; |
588 : | suxen_drol | 942 | } |
589 : | |||
590 : | edgomez | 1042 | /*---------------------------------------------------------------------------- |
591 : | *--------------------------------------------------------------------------*/ | ||
592 : | suxen_drol | 942 | |
593 : | edgomez | 1042 | static int |
594 : | rc_2pass2_after(rc_2pass2_t * rc, xvid_plg_data_t * data) | ||
595 : | suxen_drol | 942 | { |
596 : | edgomez | 1043 | const char frame_type[4] = { 'i', 'p', 'b', 's'}; |
597 : | stat_t * s = &rc->stats[data->frame_num]; | ||
598 : | suxen_drol | 942 | |
599 : | edgomez | 1037 | /* Insufficent stats data */ |
600 : | if (data->frame_num >= rc->num_frames) | ||
601 : | suxen_drol | 942 | return 0; |
602 : | |||
603 : | rc->quant_count[data->quant]++; | ||
604 : | |||
605 : | if (data->type == XVID_TYPE_IVOP) { | ||
606 : | int kfdiff = (rc->keyframe_locations[rc->KF_idx] - rc->keyframe_locations[rc->KF_idx - 1]); | ||
607 : | |||
608 : | 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 : | edgomez | 1042 | } else { |
620 : | suxen_drol | 942 | // 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 : | |||
625 : | edgomez | 1043 | DPRINTF(XVID_DEBUG_RC, "[%i] type:%c quant:%i stats1:%i scaled:%i actual:%i overflow:%i\n", |
626 : | edgomez | 1042 | data->frame_num, |
627 : | edgomez | 1043 | frame_type[data->type-1], |
628 : | edgomez | 1042 | data->quant, |
629 : | s->length, | ||
630 : | s->scaled_length, | ||
631 : | data->length, | ||
632 : | rc->overflow); | ||
633 : | suxen_drol | 942 | |
634 : | return(0); | ||
635 : | } | ||
636 : | |||
637 : | edgomez | 1042 | /***************************************************************************** |
638 : | * Helper functions definition | ||
639 : | ****************************************************************************/ | ||
640 : | suxen_drol | 942 | |
641 : | edgomez | 1042 | #define BUF_SZ 1024 |
642 : | #define MAX_COLS 5 | ||
643 : | suxen_drol | 942 | |
644 : | edgomez | 1042 | /* open stats file, and count num frames */ |
645 : | static int | ||
646 : | det_stats_length(rc_2pass2_t * rc, char * filename) | ||
647 : | suxen_drol | 942 | { |
648 : | edgomez | 1042 | FILE * f; |
649 : | int n, ignore; | ||
650 : | char type; | ||
651 : | |||
652 : | rc->num_frames = 0; | ||
653 : | rc->num_keyframes = 0; | ||
654 : | |||
655 : | if ((f = fopen(filename, "rt")) == NULL) | ||
656 : | suxen_drol | 942 | return 0; |
657 : | |||
658 : | edgomez | 1042 | 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 : | } | ||
667 : | suxen_drol | 942 | |
668 : | edgomez | 1042 | fclose(f); |
669 : | suxen_drol | 942 | |
670 : | edgomez | 1042 | return 1; |
671 : | } | ||
672 : | suxen_drol | 942 | |
673 : | edgomez | 1042 | /* open stats file(s) and read into rc->stats array */ |
674 : | |||
675 : | static int | ||
676 : | load_stats(rc_2pass2_t *rc, char * filename) | ||
677 : | { | ||
678 : | FILE * f; | ||
679 : | int i, not_scaled; | ||
680 : | |||
681 : | |||
682 : | 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 : | |||
692 : | 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 : | |||
699 : | 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 : | } | ||
709 : | |||
710 : | i++; | ||
711 : | suxen_drol | 942 | } |
712 : | |||
713 : | edgomez | 1042 | rc->num_frames = i; |
714 : | |||
715 : | fclose(f); | ||
716 : | |||
717 : | return 1; | ||
718 : | suxen_drol | 942 | } |
719 : | edgomez | 1042 | |
720 : | #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 : | |||
732 : | /* pre-process the statistics data | ||
733 : | - for each type, count, tot_length, min_length, max_length | ||
734 : | - set keyframes_locations | ||
735 : | */ | ||
736 : | |||
737 : | static void | ||
738 : | pre_process0(rc_2pass2_t * rc) | ||
739 : | { | ||
740 : | int i,j; | ||
741 : | |||
742 : | 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 : | |||
749 : | rc->max_length = INT_MIN; | ||
750 : | |||
751 : | for (i=j=0; i<rc->num_frames; i++) { | ||
752 : | stat_t * s = &rc->stats[i]; | ||
753 : | |||
754 : | 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 : | |||
761 : | if (s->length > rc->max_length) { | ||
762 : | rc->max_length = s->length; | ||
763 : | } | ||
764 : | |||
765 : | if (s->type == XVID_TYPE_IVOP) { | ||
766 : | rc->keyframe_locations[j] = i; | ||
767 : | j++; | ||
768 : | } | ||
769 : | } | ||
770 : | |||
771 : | /* | ||
772 : | * 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 : | */ | ||
778 : | rc->keyframe_locations[j] = i; | ||
779 : | |||
780 : | 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 : | |||
787 : | 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 : | |||
793 : | rc->avg_weight = 0.0; | ||
794 : | rc->tot_quant = 0; | ||
795 : | |||
796 : | |||
797 : | 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 : | |||
838 : | DPRINTF(XVID_DEBUG_RC, "center_weight: %f (for %i frames); fixed_bytes: %i\n", rc->avg_weight, n, rc->tot_quant); | ||
839 : | } | ||
840 : | |||
841 : | |||
842 : | /* scale the curve */ | ||
843 : | |||
844 : | 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 : | edgomez | 1047 | int i, num_MBs; |
851 : | int min_size[3]; | ||
852 : | edgomez | 1042 | |
853 : | /* Let's compute a linear scaler in order to perform curve scaling */ | ||
854 : | scaler = (double)target / (double)pass1_length; | ||
855 : | |||
856 : | if (target <= 0 || pass1_length <= 0 || target >= pass1_length) { | ||
857 : | DPRINTF(XVID_DEBUG_RC, "WARNING: Undersize detected\n"); | ||
858 : | scaler = 1.0; | ||
859 : | } | ||
860 : | |||
861 : | DPRINTF(XVID_DEBUG_RC, | ||
862 : | "Before correction: target=%i, tot_length=%i, scaler=%f\n", | ||
863 : | (int)target, (int)pass1_length, scaler); | ||
864 : | |||
865 : | /* | ||
866 : | edgomez | 1047 | * Compute min frame lengths (for each frame type) according to the number |
867 : | * of MBs. We sum all blocks count from frame 0 (should be an IFrame, so | ||
868 : | * blocks[0] should be enough) to know how many MBs there are. | ||
869 : | */ | ||
870 : | num_MBs = rc->stats[0].blks[0] + rc->stats[0].blks[1] + rc->stats[0].blks[2]; | ||
871 : | min_size[0] = ((num_MBs*22) + 240) / 8; | ||
872 : | min_size[1] = ((num_MBs) + 88) / 8; | ||
873 : | min_size[2] = 8; | ||
874 : | |||
875 : | /* | ||
876 : | edgomez | 1042 | * Perform an initial scale pass. |
877 : | * If a frame size is scaled underneath our hardcoded minimums, then we | ||
878 : | * force the frame size to the minimum, and deduct the original & scaled | ||
879 : | * frame length from the original and target total lengths | ||
880 : | */ | ||
881 : | for (i=0; i<rc->num_frames; i++) { | ||
882 : | stat_t * s = &rc->stats[i]; | ||
883 : | int len; | ||
884 : | |||
885 : | if (s->zone_mode == XVID_ZONE_QUANT) { | ||
886 : | s->scaled_length = s->length; | ||
887 : | continue; | ||
888 : | } | ||
889 : | |||
890 : | /* Compute teh scaled length */ | ||
891 : | len = (int)((double)s->length * scaler * s->weight / rc->avg_weight); | ||
892 : | |||
893 : | /* Compare with the computed minimum */ | ||
894 : | if (len < min_size[s->type-1]) { | ||
895 : | /* force frame size to our computed minimum */ | ||
896 : | s->scaled_length = min_size[s->type-1]; | ||
897 : | target -= s->scaled_length; | ||
898 : | pass1_length -= s->length; | ||
899 : | } else { | ||
900 : | /* Do nothing for now, we'll scale this later */ | ||
901 : | s->scaled_length = 0; | ||
902 : | } | ||
903 : | } | ||
904 : | |||
905 : | /* Correct the scaler for all non forced frames */ | ||
906 : | scaler = (double)target / (double)pass1_length; | ||
907 : | |||
908 : | /* Detect undersizing */ | ||
909 : | if (target <= 0 || pass1_length <= 0 || target >= pass1_length) { | ||
910 : | DPRINTF(XVID_DEBUG_RC, "WARNING: Undersize detected\n"); | ||
911 : | scaler = 1.0; | ||
912 : | } | ||
913 : | |||
914 : | DPRINTF(XVID_DEBUG_RC, | ||
915 : | "After correction: target=%i, tot_length=%i, scaler=%f\n", | ||
916 : | (int)target, (int)pass1_length, scaler); | ||
917 : | |||
918 : | /* Do another pass with the new scaler */ | ||
919 : | for (i=0; i<rc->num_frames; i++) { | ||
920 : | stat_t * s = &rc->stats[i]; | ||
921 : | |||
922 : | /* Ignore frame with forced frame sizes */ | ||
923 : | if (s->scaled_length == 0) | ||
924 : | s->scaled_length = (int)((double)s->length * scaler * s->weight / rc->avg_weight); | ||
925 : | } | ||
926 : | |||
927 : | } | ||
928 : | |||
929 : | static void | ||
930 : | pre_process1(rc_2pass2_t * rc) | ||
931 : | { | ||
932 : | int i; | ||
933 : | double total1, total2; | ||
934 : | uint64_t ivop_boost_total; | ||
935 : | |||
936 : | ivop_boost_total = 0; | ||
937 : | rc->curve_comp_error = 0; | ||
938 : | |||
939 : | for (i=0; i<3; i++) { | ||
940 : | rc->tot_scaled_length[i] = 0; | ||
941 : | } | ||
942 : | |||
943 : | for (i=0; i<rc->num_frames; i++) { | ||
944 : | stat_t * s = &rc->stats[i]; | ||
945 : | |||
946 : | rc->tot_scaled_length[s->type-1] += s->scaled_length; | ||
947 : | |||
948 : | if (s->type == XVID_TYPE_IVOP) { | ||
949 : | ivop_boost_total += s->scaled_length * rc->param.keyframe_boost / 100; | ||
950 : | } | ||
951 : | } | ||
952 : | |||
953 : | rc->movie_curve = ((double)(rc->tot_scaled_length[XVID_TYPE_PVOP-1] + rc->tot_scaled_length[XVID_TYPE_BVOP-1] + ivop_boost_total) / | ||
954 : | (rc->tot_scaled_length[XVID_TYPE_PVOP-1] + rc->tot_scaled_length[XVID_TYPE_BVOP-1])); | ||
955 : | |||
956 : | for(i=0; i<3; i++) { | ||
957 : | if (rc->count[i] == 0 || rc->movie_curve == 0) { | ||
958 : | rc->avg_length[i] = 1; | ||
959 : | }else{ | ||
960 : | rc->avg_length[i] = rc->tot_scaled_length[i] / rc->count[i] / rc->movie_curve; | ||
961 : | } | ||
962 : | } | ||
963 : | |||
964 : | /* --- */ | ||
965 : | |||
966 : | total1=total2=0; | ||
967 : | |||
968 : | for (i=0; i<rc->num_frames; i++) { | ||
969 : | stat_t * s = &rc->stats[i]; | ||
970 : | |||
971 : | if (s->type != XVID_TYPE_IVOP) { | ||
972 : | double dbytes,dbytes2; | ||
973 : | |||
974 : | dbytes = s->scaled_length / rc->movie_curve; | ||
975 : | dbytes2 = 0; /* XXX: warning */ | ||
976 : | total1 += dbytes; | ||
977 : | if (s->type == XVID_TYPE_BVOP) | ||
978 : | dbytes *= rc->avg_length[XVID_TYPE_PVOP-1] / rc->avg_length[XVID_TYPE_BVOP-1]; | ||
979 : | |||
980 : | edgomez | 1046 | if (dbytes > rc->avg_length[XVID_TYPE_PVOP-1]) { |
981 : | dbytes2=((double)dbytes + (rc->avg_length[XVID_TYPE_PVOP-1] - dbytes) * rc->param.curve_compression_high / 100.0); | ||
982 : | } else { | ||
983 : | dbytes2 = ((double)dbytes + (rc->avg_length[XVID_TYPE_PVOP-1] - dbytes) * rc->param.curve_compression_low / 100.0); | ||
984 : | } | ||
985 : | edgomez | 1042 | |
986 : | if (s->type == XVID_TYPE_BVOP) { | ||
987 : | dbytes2 *= rc->avg_length[XVID_TYPE_BVOP-1] / rc->avg_length[XVID_TYPE_PVOP-1]; | ||
988 : | if (dbytes2 < rc->min_length[XVID_TYPE_BVOP-1]) | ||
989 : | dbytes2 = rc->min_length[XVID_TYPE_BVOP-1]; | ||
990 : | }else{ | ||
991 : | if (dbytes2 < rc->min_length[XVID_TYPE_PVOP-1]) | ||
992 : | dbytes2 = rc->min_length[XVID_TYPE_PVOP-1]; | ||
993 : | } | ||
994 : | total2 += dbytes2; | ||
995 : | } | ||
996 : | } | ||
997 : | |||
998 : | rc->curve_comp_scale = total1 / total2; | ||
999 : | |||
1000 : | edgomez | 1046 | DPRINTF(XVID_DEBUG_RC, "middle frame size for asymmetric curve compression: %i\n", |
1001 : | edgomez | 1042 | (int)(rc->avg_length[XVID_TYPE_PVOP-1] * rc->curve_comp_scale)); |
1002 : | |||
1003 : | rc->overflow = 0; | ||
1004 : | rc->KFoverflow = 0; | ||
1005 : | rc->KFoverflow_partial = 0; | ||
1006 : | rc->KF_idx = 1; | ||
1007 : | } |
No admin address has been configured | ViewVC Help |
Powered by ViewVC 1.0.4 |