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