Parent Directory | Revision Log
Revision 1032 - (view) (download)
1 : | suxen_drol | 942 | /****************************************************************************** |
2 : | * | ||
3 : | * XviD Bit Rate Controller Library | ||
4 : | * - VBR 2 pass bitrate controler implementation - | ||
5 : | * | ||
6 : | * Copyright (C) 2002 Edouard Gomez <ed.gomez@wanadoo.fr> | ||
7 : | * | ||
8 : | * The curve treatment algorithm is the one implemented by Foxer <email?> and | ||
9 : | * Dirk Knop <dknop@gwdg.de> for the XviD vfw dynamic library. | ||
10 : | * | ||
11 : | * This program is free software; you can redistribute it and/or modify | ||
12 : | * it under the terms of the GNU General Public License as published by | ||
13 : | * the Free Software Foundation; either version 2 of the License, or | ||
14 : | * (at your option) any later version. | ||
15 : | * | ||
16 : | * This program is distributed in the hope that it will be useful, | ||
17 : | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
18 : | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
19 : | * GNU General Public License for more details. | ||
20 : | * | ||
21 : | * You should have received a copy of the GNU General Public License | ||
22 : | * along with this program; if not, write to the Free Software | ||
23 : | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
24 : | * | ||
25 : | suxen_drol | 1032 | * $Id: plugin_2pass2.c,v 1.1.2.5 2003-05-17 13:36:38 suxen_drol Exp $ |
26 : | suxen_drol | 942 | * |
27 : | *****************************************************************************/ | ||
28 : | |||
29 : | #include <stdio.h> | ||
30 : | #include <math.h> | ||
31 : | |||
32 : | #define RAD2DEG 57.295779513082320876798154814105 | ||
33 : | #define DEG2RAD 0.017453292519943295769236907684886 | ||
34 : | |||
35 : | #include "../xvid.h" | ||
36 : | #include "../image/image.h" | ||
37 : | |||
38 : | typedef struct { | ||
39 : | int type; /* first pass type */ | ||
40 : | int quant; /* first pass quant */ | ||
41 : | suxen_drol | 977 | int blks[3]; /* k,m,y blks */ |
42 : | suxen_drol | 942 | int length; /* first pass length */ |
43 : | suxen_drol | 1030 | int scaled_length; /* scaled length */ |
44 : | int desired_length; /* desired length; calcuated during encoding */ | ||
45 : | |||
46 : | int zone_mode; /* XVID_ZONE_xxx */ | ||
47 : | double weight; | ||
48 : | suxen_drol | 942 | } stat_t; |
49 : | |||
50 : | |||
51 : | |||
52 : | |||
53 : | /* context struct */ | ||
54 : | typedef struct | ||
55 : | { | ||
56 : | xvid_plugin_2pass2_t param; | ||
57 : | |||
58 : | /* constant statistical data */ | ||
59 : | suxen_drol | 977 | int num_frames; |
60 : | suxen_drol | 942 | int num_keyframes; |
61 : | suxen_drol | 1030 | uint64_t target; /* target filesize */ |
62 : | suxen_drol | 977 | |
63 : | suxen_drol | 942 | int count[3]; /* count of each frame types */ |
64 : | uint64_t tot_length[3]; /* total length of each frame types */ | ||
65 : | double avg_length[3]; /* avg */ | ||
66 : | int min_length[3]; /* min frame length of each frame types */ | ||
67 : | uint64_t tot_scaled_length[3]; /* total scaled length of each frame type */ | ||
68 : | int max_length; /* max frame size */ | ||
69 : | suxen_drol | 1030 | |
70 : | /* zone statistical data */ | ||
71 : | double avg_weight; /* average weight */ | ||
72 : | int64_t tot_quant; /* total length used by XVID_ZONE_QUANT zones */ | ||
73 : | |||
74 : | |||
75 : | suxen_drol | 942 | double curve_comp_scale; |
76 : | double movie_curve; | ||
77 : | |||
78 : | double alt_curve_low; | ||
79 : | double alt_curve_high; | ||
80 : | double alt_curve_low_diff; | ||
81 : | double alt_curve_high_diff; | ||
82 : | double alt_curve_curve_bias_bonus; | ||
83 : | double alt_curve_mid_qual; | ||
84 : | double alt_curve_qual_dev; | ||
85 : | |||
86 : | /* dynamic */ | ||
87 : | |||
88 : | int * keyframe_locations; | ||
89 : | stat_t * stats; | ||
90 : | |||
91 : | double pquant_error[32]; | ||
92 : | double bquant_error[32]; | ||
93 : | int quant_count[32]; | ||
94 : | int last_quant[3]; | ||
95 : | |||
96 : | double curve_comp_error; | ||
97 : | int overflow; | ||
98 : | int KFoverflow; | ||
99 : | int KFoverflow_partial; | ||
100 : | int KF_idx; | ||
101 : | suxen_drol | 1030 | |
102 : | double fq_error; | ||
103 : | suxen_drol | 942 | } rc_2pass2_t; |
104 : | |||
105 : | |||
106 : | |||
107 : | #define BUF_SZ 1024 | ||
108 : | #define MAX_COLS 5 | ||
109 : | |||
110 : | |||
111 : | /* open stats file, and count num frames */ | ||
112 : | |||
113 : | static int det_stats_length(rc_2pass2_t * rc, char * filename) | ||
114 : | { | ||
115 : | FILE * f; | ||
116 : | int n, ignore; | ||
117 : | char type; | ||
118 : | |||
119 : | rc->num_frames = 0; | ||
120 : | rc->num_keyframes = 0; | ||
121 : | |||
122 : | if ((f = fopen(filename, "rt")) == NULL) | ||
123 : | return 0; | ||
124 : | |||
125 : | suxen_drol | 977 | while((n = fscanf(f, "%c %d %d %d %d %d %d\n", |
126 : | &type, &ignore, &ignore, &ignore, &ignore, &ignore, &ignore)) != EOF) { | ||
127 : | suxen_drol | 942 | if (type == 'i') { |
128 : | rc->num_frames++; | ||
129 : | rc->num_keyframes++; | ||
130 : | }else if (type == 'p' || type == 'b' || type == 's') { | ||
131 : | rc->num_frames++; | ||
132 : | } | ||
133 : | } | ||
134 : | |||
135 : | fclose(f); | ||
136 : | |||
137 : | return 1; | ||
138 : | } | ||
139 : | |||
140 : | |||
141 : | |||
142 : | suxen_drol | 977 | /* open stats file(s) and read into rc->stats array */ |
143 : | suxen_drol | 942 | |
144 : | suxen_drol | 977 | static int load_stats(rc_2pass2_t *rc, char * filename) |
145 : | { | ||
146 : | FILE * f; | ||
147 : | int i, not_scaled; | ||
148 : | |||
149 : | |||
150 : | if ((f = fopen(filename, "rt"))==NULL) | ||
151 : | return 0; | ||
152 : | |||
153 : | i = 0; | ||
154 : | not_scaled = 0; | ||
155 : | while(i < rc->num_frames) { | ||
156 : | stat_t * s = &rc->stats[i]; | ||
157 : | int n; | ||
158 : | char type; | ||
159 : | |||
160 : | s->scaled_length = 0; | ||
161 : | 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); | ||
162 : | if (n == EOF) break; | ||
163 : | if (n < 7) { | ||
164 : | not_scaled = 1; | ||
165 : | } | ||
166 : | |||
167 : | if (type == 'i') { | ||
168 : | s->type = XVID_TYPE_IVOP; | ||
169 : | }else if (type == 'p' || type == 's') { | ||
170 : | s->type = XVID_TYPE_PVOP; | ||
171 : | }else if (type == 'b') { | ||
172 : | s->type = XVID_TYPE_BVOP; | ||
173 : | }else{ /* unknown type */ | ||
174 : | suxen_drol | 1032 | DPRINTF(XVID_DEBUG_RC, "unknown stats frame type; assuming pvop"); |
175 : | s->type = XVID_TYPE_PVOP; | ||
176 : | suxen_drol | 977 | } |
177 : | |||
178 : | i++; | ||
179 : | } | ||
180 : | rc->num_frames = i; | ||
181 : | |||
182 : | fclose(f); | ||
183 : | |||
184 : | return 1; | ||
185 : | } | ||
186 : | |||
187 : | |||
188 : | |||
189 : | suxen_drol | 942 | static void print_stats(rc_2pass2_t * rc) |
190 : | { | ||
191 : | int i; | ||
192 : | for (i = 0; i < rc->num_frames; i++) { | ||
193 : | stat_t * s = &rc->stats[i]; | ||
194 : | suxen_drol | 1032 | DPRINTF(XVID_DEBUG_RC, "%i %i %i %i\n", s->type, s->quant, s->length, s->scaled_length); |
195 : | suxen_drol | 942 | |
196 : | } | ||
197 : | } | ||
198 : | |||
199 : | |||
200 : | /* pre-process the statistics data | ||
201 : | suxen_drol | 1030 | - for each type, count, tot_length, min_length, max_length |
202 : | - set keyframes_locations | ||
203 : | suxen_drol | 942 | */ |
204 : | |||
205 : | suxen_drol | 977 | void pre_process0(rc_2pass2_t * rc) |
206 : | suxen_drol | 942 | { |
207 : | int i,j; | ||
208 : | |||
209 : | for (i=0; i<3; i++) { | ||
210 : | rc->count[i]=0; | ||
211 : | rc->tot_length[i] = 0; | ||
212 : | rc->last_quant[i] = 0; | ||
213 : | } | ||
214 : | |||
215 : | for (i=j=0; i<rc->num_frames; i++) { | ||
216 : | stat_t * s = &rc->stats[i]; | ||
217 : | |||
218 : | rc->count[s->type-1]++; | ||
219 : | rc->tot_length[s->type-1] += s->length; | ||
220 : | |||
221 : | if (i == 0 || s->length < rc->min_length[s->type-1]) { | ||
222 : | rc->min_length[s->type-1] = s->length; | ||
223 : | } | ||
224 : | |||
225 : | if (i == 0 || s->length > rc->max_length) { | ||
226 : | rc->max_length = s->length; | ||
227 : | } | ||
228 : | |||
229 : | if (s->type == XVID_TYPE_IVOP) { | ||
230 : | rc->keyframe_locations[j] = i; | ||
231 : | j++; | ||
232 : | } | ||
233 : | } | ||
234 : | rc->keyframe_locations[j] = i; | ||
235 : | suxen_drol | 977 | } |
236 : | suxen_drol | 942 | |
237 : | suxen_drol | 1030 | |
238 : | /* calculate zone weight "center" */ | ||
239 : | suxen_drol | 977 | |
240 : | suxen_drol | 1030 | static void zone_process(rc_2pass2_t *rc, const xvid_plg_create_t * create) |
241 : | { | ||
242 : | int i,j; | ||
243 : | int n = 0; | ||
244 : | suxen_drol | 977 | |
245 : | suxen_drol | 1030 | rc->avg_weight = 0.0; |
246 : | rc->tot_quant = 0; | ||
247 : | |||
248 : | |||
249 : | if (create->num_zones == 0) { | ||
250 : | for (j = 0; j < rc->num_frames; j++) { | ||
251 : | rc->stats[j].zone_mode = XVID_ZONE_WEIGHT; | ||
252 : | rc->stats[j].weight = 1.0; | ||
253 : | } | ||
254 : | rc->avg_weight += rc->num_frames * 1.0; | ||
255 : | n += rc->num_frames; | ||
256 : | } | ||
257 : | |||
258 : | |||
259 : | for(i=0; i < create->num_zones; i++) { | ||
260 : | |||
261 : | int next = (i+1<create->num_zones) ? create->zones[i+1].frame : rc->num_frames; | ||
262 : | |||
263 : | if (i==0 && create->zones[i].frame > 0) { | ||
264 : | for (j = 0; j < create->zones[i].frame && j < rc->num_frames; j++) { | ||
265 : | rc->stats[j].zone_mode = XVID_ZONE_WEIGHT; | ||
266 : | rc->stats[j].weight = 1.0; | ||
267 : | } | ||
268 : | rc->avg_weight += create->zones[i].frame * 1.0; | ||
269 : | n += create->zones[i].frame; | ||
270 : | } | ||
271 : | |||
272 : | if (create->zones[i].mode == XVID_ZONE_WEIGHT) { | ||
273 : | for (j = create->zones[i].frame; j < next && j < rc->num_frames; j++ ) { | ||
274 : | rc->stats[j].zone_mode = XVID_ZONE_WEIGHT; | ||
275 : | rc->stats[j].weight = (double)create->zones[i].increment / (double)create->zones[i].base; | ||
276 : | } | ||
277 : | next -= create->zones[i].frame; | ||
278 : | rc->avg_weight += (double)(next * create->zones[i].increment) / (double)create->zones[i].base; | ||
279 : | n += next; | ||
280 : | }else{ // XVID_ZONE_QUANT | ||
281 : | for (j = create->zones[i].frame; j < next && j < rc->num_frames; j++ ) { | ||
282 : | rc->stats[j].zone_mode = XVID_ZONE_QUANT; | ||
283 : | rc->stats[j].weight = (double)create->zones[i].increment / (double)create->zones[i].base; | ||
284 : | rc->tot_quant += rc->stats[j].length; | ||
285 : | } | ||
286 : | } | ||
287 : | } | ||
288 : | rc->avg_weight = n>0 ? rc->avg_weight/n : 1.0; | ||
289 : | |||
290 : | suxen_drol | 1032 | DPRINTF(XVID_DEBUG_RC, "center_weight: %f (for %i frames); fixed_bytes: %i\n", rc->avg_weight, n, rc->tot_quant); |
291 : | suxen_drol | 1030 | } |
292 : | |||
293 : | |||
294 : | /* scale the curve */ | ||
295 : | |||
296 : | static void internal_scale(rc_2pass2_t *rc) | ||
297 : | { | ||
298 : | int64_t target = rc->target - rc->tot_quant; | ||
299 : | int64_t pass1_length = rc->tot_length[0] + rc->tot_length[1] + rc->tot_length[2] - rc->tot_quant; | ||
300 : | int min_size[3]; | ||
301 : | double scaler; | ||
302 : | int i; | ||
303 : | |||
304 : | |||
305 : | /* perform an initial scale pass. | ||
306 : | if a frame size is scaled underneath our hardcoded minimums, then we force the | ||
307 : | frame size to the minimum, and deduct the original & scaled frmae length from the | ||
308 : | original and target total lengths */ | ||
309 : | |||
310 : | min_size[0] = ((rc->stats[0].blks[0]*22) + 240) / 8; | ||
311 : | min_size[1] = (rc->stats[0].blks[0] + 88) / 8; | ||
312 : | min_size[2] = 8; | ||
313 : | |||
314 : | scaler = (double)target / (double)pass1_length; | ||
315 : | |||
316 : | suxen_drol | 1032 | if (target <= 0 || pass1_length <= 0 || target >= pass1_length) { |
317 : | DPRINTF(XVID_DEBUG_RC, "undersize warning\n"); | ||
318 : | scaler = 1.0; | ||
319 : | } | ||
320 : | |||
321 : | DPRINTF(XVID_DEBUG_RC, "target=%i, tot_length=%i, scaler=%f\n", (int)target, (int)pass1_length, scaler); | ||
322 : | |||
323 : | suxen_drol | 1030 | for (i=0; i<rc->num_frames; i++) { |
324 : | stat_t * s = &rc->stats[i]; | ||
325 : | int len; | ||
326 : | |||
327 : | if (s->zone_mode == XVID_ZONE_QUANT) { | ||
328 : | s->scaled_length = s->length; | ||
329 : | }else { | ||
330 : | len = (int)((double)s->length * scaler * s->weight / rc->avg_weight); | ||
331 : | if (len < min_size[s->type]) { /* force frame size */ | ||
332 : | s->scaled_length = min_size[s->type]; | ||
333 : | target -= s->scaled_length; | ||
334 : | pass1_length -= s->length; | ||
335 : | }else{ | ||
336 : | s->scaled_length = 0; | ||
337 : | } | ||
338 : | } | ||
339 : | } | ||
340 : | |||
341 : | suxen_drol | 1032 | scaler = (double)target / (double)pass1_length; |
342 : | if (target <= 0 || pass1_length <= 0 || target >= pass1_length) { | ||
343 : | DPRINTF(XVID_DEBUG_RC,"undersize warning\n"); | ||
344 : | scaler = 1.0; | ||
345 : | suxen_drol | 1030 | } |
346 : | |||
347 : | suxen_drol | 1032 | DPRINTF(XVID_DEBUG_RC, "target=%i, tot_length=%i, scaler=%f\n", (int)target, (int)pass1_length, scaler); |
348 : | suxen_drol | 1030 | |
349 : | for (i=0; i<rc->num_frames; i++) { | ||
350 : | stat_t * s = &rc->stats[i]; | ||
351 : | |||
352 : | if (s->scaled_length==0) { /* ignore frame with forced frame sizes */ | ||
353 : | s->scaled_length = (int)((double)s->length * scaler * s->weight / rc->avg_weight); | ||
354 : | } | ||
355 : | } | ||
356 : | } | ||
357 : | |||
358 : | |||
359 : | |||
360 : | |||
361 : | suxen_drol | 977 | void pre_process1(rc_2pass2_t * rc) |
362 : | { | ||
363 : | int i; | ||
364 : | double total1, total2; | ||
365 : | uint64_t ivop_boost_total; | ||
366 : | |||
367 : | ivop_boost_total = 0; | ||
368 : | rc->curve_comp_error = 0; | ||
369 : | |||
370 : | for (i=0; i<3; i++) { | ||
371 : | rc->tot_scaled_length[i] = 0; | ||
372 : | } | ||
373 : | |||
374 : | for (i=0; i<rc->num_frames; i++) { | ||
375 : | stat_t * s = &rc->stats[i]; | ||
376 : | |||
377 : | rc->tot_scaled_length[s->type-1] += s->scaled_length; | ||
378 : | |||
379 : | if (s->type == XVID_TYPE_IVOP) { | ||
380 : | ivop_boost_total += s->scaled_length * rc->param.keyframe_boost / 100; | ||
381 : | } | ||
382 : | } | ||
383 : | |||
384 : | suxen_drol | 942 | rc->movie_curve = ((double)(rc->tot_scaled_length[XVID_TYPE_PVOP-1] + rc->tot_scaled_length[XVID_TYPE_BVOP-1] + ivop_boost_total) / |
385 : | (rc->tot_scaled_length[XVID_TYPE_PVOP-1] + rc->tot_scaled_length[XVID_TYPE_BVOP-1])); | ||
386 : | |||
387 : | for(i=0; i<3; i++) { | ||
388 : | if (rc->count[i] == 0 || rc->movie_curve == 0) { | ||
389 : | rc->avg_length[i] = 1; | ||
390 : | }else{ | ||
391 : | rc->avg_length[i] = rc->tot_scaled_length[i] / rc->count[i] / rc->movie_curve; | ||
392 : | } | ||
393 : | } | ||
394 : | |||
395 : | /* alt curve stuff here */ | ||
396 : | |||
397 : | if (rc->param.use_alt_curve) { | ||
398 : | const double avg_pvop = rc->avg_length[XVID_TYPE_PVOP-1]; | ||
399 : | const uint64_t tot_pvop = rc->tot_length[XVID_TYPE_PVOP-1]; | ||
400 : | const uint64_t tot_bvop = rc->tot_length[XVID_TYPE_BVOP-1]; | ||
401 : | const uint64_t tot_scaled_pvop = rc->tot_scaled_length[XVID_TYPE_PVOP-1]; | ||
402 : | const uint64_t tot_scaled_bvop = rc->tot_scaled_length[XVID_TYPE_BVOP-1]; | ||
403 : | |||
404 : | rc->alt_curve_low = avg_pvop - avg_pvop * (double)rc->param.alt_curve_low_dist / 100.0; | ||
405 : | rc->alt_curve_low_diff = avg_pvop - rc->alt_curve_low; | ||
406 : | rc->alt_curve_high = avg_pvop + avg_pvop * (double)rc->param.alt_curve_high_dist / 100.0; | ||
407 : | rc->alt_curve_high_diff = rc->alt_curve_high - avg_pvop; | ||
408 : | |||
409 : | if (rc->param.alt_curve_use_auto) { | ||
410 : | if (tot_bvop + tot_pvop > tot_scaled_bvop + tot_scaled_pvop) { | ||
411 : | rc->param.alt_curve_min_rel_qual = (int)(100.0 - (100.0 - 100.0 / | ||
412 : | ((double)(tot_pvop + tot_bvop) / (double)(tot_scaled_pvop + tot_scaled_bvop))) * (double)rc->param.alt_curve_auto_str / 100.0); | ||
413 : | |||
414 : | if (rc->param.alt_curve_min_rel_qual < 20) | ||
415 : | rc->param.alt_curve_min_rel_qual = 20; | ||
416 : | }else{ | ||
417 : | rc->param.alt_curve_min_rel_qual = 100; | ||
418 : | } | ||
419 : | } | ||
420 : | rc->alt_curve_mid_qual = (1.0 + (double)rc->param.alt_curve_min_rel_qual / 100.0) / 2.0; | ||
421 : | rc->alt_curve_qual_dev = 1.0 - rc->alt_curve_mid_qual; | ||
422 : | |||
423 : | if (rc->param.alt_curve_low_dist > 100) { | ||
424 : | switch(rc->param.alt_curve_type) { | ||
425 : | case XVID_CURVE_SINE: // Sine Curve (high aggressiveness) | ||
426 : | rc->alt_curve_qual_dev *= 2.0 / (1.0 + sin(DEG2RAD * (avg_pvop * 90.0 / rc->alt_curve_low_diff))); | ||
427 : | rc->alt_curve_mid_qual = 1.0 - rc->alt_curve_qual_dev * sin(DEG2RAD * (avg_pvop * 90.0 / rc->alt_curve_low_diff)); | ||
428 : | break; | ||
429 : | case XVID_CURVE_LINEAR: // Linear (medium aggressiveness) | ||
430 : | rc->alt_curve_qual_dev *= 2.0 / (1.0 + avg_pvop / rc->alt_curve_low_diff); | ||
431 : | rc->alt_curve_mid_qual = 1.0 - rc->alt_curve_qual_dev * avg_pvop / rc->alt_curve_low_diff; | ||
432 : | break; | ||
433 : | case XVID_CURVE_COSINE: // Cosine Curve (low aggressiveness) | ||
434 : | rc->alt_curve_qual_dev *= 2.0 / (1.0 + (1.0 - cos(DEG2RAD * (avg_pvop * 90.0 / rc->alt_curve_low_diff)))); | ||
435 : | rc->alt_curve_mid_qual = 1.0 - rc->alt_curve_qual_dev * (1.0 - cos(DEG2RAD * (avg_pvop * 90.0 / rc->alt_curve_low_diff))); | ||
436 : | } | ||
437 : | } | ||
438 : | } | ||
439 : | /* --- */ | ||
440 : | |||
441 : | |||
442 : | total1=total2=0; | ||
443 : | suxen_drol | 977 | for (i=0; i<rc->num_frames; i++) { |
444 : | suxen_drol | 942 | stat_t * s = &rc->stats[i]; |
445 : | |||
446 : | if (s->type != XVID_TYPE_IVOP) { | ||
447 : | double dbytes,dbytes2; | ||
448 : | |||
449 : | dbytes = s->scaled_length / rc->movie_curve; | ||
450 : | dbytes2 = 0; /* XXX: warning */ | ||
451 : | total1 += dbytes; | ||
452 : | if (s->type == XVID_TYPE_BVOP) | ||
453 : | dbytes *= rc->avg_length[XVID_TYPE_PVOP-1] / rc->avg_length[XVID_TYPE_BVOP-1]; | ||
454 : | |||
455 : | if (rc->param.use_alt_curve) { | ||
456 : | if (dbytes > rc->avg_length[XVID_TYPE_PVOP-1]) { | ||
457 : | |||
458 : | if (dbytes >= rc->alt_curve_high) { | ||
459 : | dbytes2 = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev); | ||
460 : | }else{ | ||
461 : | switch(rc->param.alt_curve_type) { | ||
462 : | case XVID_CURVE_SINE : | ||
463 : | dbytes2 = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * sin(DEG2RAD * ((dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) * 90.0 / rc->alt_curve_high_diff))); | ||
464 : | break; | ||
465 : | case XVID_CURVE_LINEAR : | ||
466 : | dbytes2 = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * (dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) / rc->alt_curve_high_diff); | ||
467 : | break; | ||
468 : | case XVID_CURVE_COSINE : | ||
469 : | dbytes2 = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * (1.0 - cos(DEG2RAD * ((dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) * 90.0 / rc->alt_curve_high_diff)))); | ||
470 : | } | ||
471 : | } | ||
472 : | }else{ | ||
473 : | if (dbytes <= rc->alt_curve_low) { | ||
474 : | dbytes2 = dbytes; | ||
475 : | }else{ | ||
476 : | switch(rc->param.alt_curve_type) { | ||
477 : | case XVID_CURVE_SINE : | ||
478 : | dbytes2 = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * sin(DEG2RAD * ((dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) * 90.0 / rc->alt_curve_low_diff))); | ||
479 : | break; | ||
480 : | case XVID_CURVE_LINEAR : | ||
481 : | dbytes2 = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * (dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) / rc->alt_curve_low_diff); | ||
482 : | break; | ||
483 : | case XVID_CURVE_COSINE : | ||
484 : | dbytes2 = dbytes * (rc->alt_curve_mid_qual + rc->alt_curve_qual_dev * (1.0 - cos(DEG2RAD * ((dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) * 90.0 / rc->alt_curve_low_diff)))); | ||
485 : | } | ||
486 : | } | ||
487 : | |||
488 : | } | ||
489 : | |||
490 : | |||
491 : | }else{ | ||
492 : | if (dbytes > rc->avg_length[XVID_TYPE_PVOP-1]) { | ||
493 : | dbytes2=((double)dbytes + (rc->avg_length[XVID_TYPE_PVOP-1] - dbytes) * rc->param.curve_compression_high / 100.0); | ||
494 : | }else{ | ||
495 : | dbytes2 = ((double)dbytes + (rc->avg_length[XVID_TYPE_PVOP-1] - dbytes) * rc->param.curve_compression_low / 100.0); | ||
496 : | } | ||
497 : | } | ||
498 : | |||
499 : | if (s->type == XVID_TYPE_BVOP) { | ||
500 : | dbytes2 *= rc->avg_length[XVID_TYPE_BVOP-1] / rc->avg_length[XVID_TYPE_PVOP-1]; | ||
501 : | if (dbytes2 < rc->min_length[XVID_TYPE_BVOP-1]) | ||
502 : | dbytes2 = rc->min_length[XVID_TYPE_BVOP-1]; | ||
503 : | }else{ | ||
504 : | if (dbytes2 < rc->min_length[XVID_TYPE_PVOP-1]) | ||
505 : | dbytes2 = rc->min_length[XVID_TYPE_PVOP-1]; | ||
506 : | } | ||
507 : | total2 += dbytes2; | ||
508 : | } | ||
509 : | } | ||
510 : | |||
511 : | rc->curve_comp_scale = total1 / total2; | ||
512 : | |||
513 : | if (!rc->param.use_alt_curve) { | ||
514 : | suxen_drol | 1032 | DPRINTF(XVID_DEBUG_RC, "middle frame size for asymmetric curve compression: %i\n", |
515 : | suxen_drol | 942 | (int)(rc->avg_length[XVID_TYPE_PVOP-1] * rc->curve_comp_scale)); |
516 : | } | ||
517 : | |||
518 : | if (rc->param.use_alt_curve) { | ||
519 : | int bonus_bias = rc->param.alt_curve_bonus_bias; | ||
520 : | int oldquant = 1; | ||
521 : | |||
522 : | if (rc->param.alt_curve_use_auto_bonus_bias) | ||
523 : | bonus_bias = rc->param.alt_curve_min_rel_qual; | ||
524 : | |||
525 : | rc->alt_curve_curve_bias_bonus = (total1 - total2) * (double)bonus_bias / 100.0 / (double)(rc->num_frames /* - credits_frames */ - rc->num_keyframes); | ||
526 : | rc->curve_comp_scale = ((total1 - total2) * (1.0 - (double)bonus_bias / 100.0) + total2) / total2; | ||
527 : | |||
528 : | |||
529 : | /* special info for alt curve: bias bonus and quantizer thresholds */ | ||
530 : | |||
531 : | suxen_drol | 1032 | DPRINTF(XVID_DEBUG_RC, "avg scaled framesize:%i", (int)rc->avg_length[XVID_TYPE_PVOP-1]); |
532 : | DPRINTF(XVID_DEBUG_RC, "bias bonus:%i bytes", (int)rc->alt_curve_curve_bias_bonus); | ||
533 : | suxen_drol | 942 | |
534 : | for (i=1; i <= (int)(rc->alt_curve_high*2)+1; i++) { | ||
535 : | double curve_temp, dbytes; | ||
536 : | int newquant; | ||
537 : | |||
538 : | dbytes = i; | ||
539 : | if (dbytes > rc->avg_length[XVID_TYPE_PVOP-1]) { | ||
540 : | if (dbytes >= rc->alt_curve_high) { | ||
541 : | curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev); | ||
542 : | }else{ | ||
543 : | switch(rc->param.alt_curve_type) | ||
544 : | { | ||
545 : | case XVID_CURVE_SINE : | ||
546 : | curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * sin(DEG2RAD * ((dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) * 90.0 / rc->alt_curve_high_diff))); | ||
547 : | break; | ||
548 : | case XVID_CURVE_LINEAR : | ||
549 : | curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * (dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) / rc->alt_curve_high_diff); | ||
550 : | break; | ||
551 : | case XVID_CURVE_COSINE : | ||
552 : | curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * (1.0 - cos(DEG2RAD * ((dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) * 90.0 / rc->alt_curve_high_diff)))); | ||
553 : | } | ||
554 : | } | ||
555 : | }else{ | ||
556 : | if (dbytes <= rc->alt_curve_low) { | ||
557 : | curve_temp = dbytes; | ||
558 : | }else{ | ||
559 : | switch(rc->param.alt_curve_type) | ||
560 : | { | ||
561 : | case XVID_CURVE_SINE : | ||
562 : | curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * sin(DEG2RAD * ((dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) * 90.0 / rc->alt_curve_low_diff))); | ||
563 : | break; | ||
564 : | case XVID_CURVE_LINEAR : | ||
565 : | curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * (dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) / rc->alt_curve_low_diff); | ||
566 : | break; | ||
567 : | case XVID_CURVE_COSINE : | ||
568 : | curve_temp = dbytes * (rc->alt_curve_mid_qual + rc->alt_curve_qual_dev * (1.0 - cos(DEG2RAD * ((dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) * 90.0 / rc->alt_curve_low_diff)))); | ||
569 : | } | ||
570 : | } | ||
571 : | } | ||
572 : | |||
573 : | if (rc->movie_curve > 1.0) | ||
574 : | dbytes *= rc->movie_curve; | ||
575 : | |||
576 : | newquant = (int)(dbytes * 2.0 / (curve_temp * rc->curve_comp_scale + rc->alt_curve_curve_bias_bonus)); | ||
577 : | if (newquant > 1) { | ||
578 : | if (newquant != oldquant) { | ||
579 : | int percent = (int)((i - rc->avg_length[XVID_TYPE_PVOP-1]) * 100.0 / rc->avg_length[XVID_TYPE_PVOP-1]); | ||
580 : | oldquant = newquant; | ||
581 : | suxen_drol | 1032 | DPRINTF(XVID_DEBUG_RC, "quant:%i threshold at %i : %i percent", newquant, i, percent); |
582 : | suxen_drol | 942 | } |
583 : | } | ||
584 : | } | ||
585 : | |||
586 : | } | ||
587 : | |||
588 : | rc->overflow = 0; | ||
589 : | rc->KFoverflow = 0; | ||
590 : | rc->KFoverflow_partial = 0; | ||
591 : | rc->KF_idx = 1; | ||
592 : | } | ||
593 : | |||
594 : | |||
595 : | |||
596 : | |||
597 : | static int rc_2pass2_create(xvid_plg_create_t * create, rc_2pass2_t ** handle) | ||
598 : | { | ||
599 : | xvid_plugin_2pass2_t * param = (xvid_plugin_2pass2_t *)create->param; | ||
600 : | rc_2pass2_t * rc; | ||
601 : | suxen_drol | 1030 | int i; |
602 : | suxen_drol | 942 | |
603 : | rc = malloc(sizeof(rc_2pass2_t)); | ||
604 : | if (rc == NULL) | ||
605 : | return XVID_ERR_MEMORY; | ||
606 : | |||
607 : | rc->param = *param; | ||
608 : | |||
609 : | if (rc->param.keyframe_boost <= 0) rc->param.keyframe_boost = 0; | ||
610 : | if (rc->param.payback_method <= 0) rc->param.payback_method = XVID_PAYBACK_PROP; | ||
611 : | if (rc->param.bitrate_payback_delay <= 0) rc->param.bitrate_payback_delay = 250; | ||
612 : | if (rc->param.curve_compression_high <= 0) rc->param.curve_compression_high = 0; | ||
613 : | if (rc->param.curve_compression_low <= 0) rc->param.curve_compression_low = 0; | ||
614 : | if (rc->param.max_overflow_improvement <= 0) rc->param.max_overflow_improvement = 60; | ||
615 : | if (rc->param.max_overflow_degradation <= 0) rc->param.max_overflow_degradation = 60; | ||
616 : | |||
617 : | if (rc->param.use_alt_curve <= 0) rc->param.use_alt_curve = 0; | ||
618 : | if (rc->param.alt_curve_high_dist <= 0) rc->param.alt_curve_high_dist = 500; | ||
619 : | if (rc->param.alt_curve_low_dist <= 0) rc->param.alt_curve_low_dist = 90; | ||
620 : | if (rc->param.alt_curve_use_auto <= 0) rc->param.alt_curve_use_auto = 1; | ||
621 : | if (rc->param.alt_curve_auto_str <= 0) rc->param.alt_curve_auto_str = 30; | ||
622 : | if (rc->param.alt_curve_type <= 0) rc->param.alt_curve_type = XVID_CURVE_LINEAR; | ||
623 : | if (rc->param.alt_curve_min_rel_qual <= 0) rc->param.alt_curve_min_rel_qual = 50; | ||
624 : | if (rc->param.alt_curve_use_auto_bonus_bias <= 0) rc->param.alt_curve_use_auto_bonus_bias = 1; | ||
625 : | if (rc->param.alt_curve_bonus_bias <= 0) rc->param.alt_curve_bonus_bias = 50; | ||
626 : | |||
627 : | if (rc->param.kftreshold <= 0) rc->param.kftreshold = 10; | ||
628 : | if (rc->param.kfreduction <= 0) rc->param.kfreduction = 20; | ||
629 : | if (rc->param.min_key_interval <= 0) rc->param.min_key_interval = 300; | ||
630 : | |||
631 : | suxen_drol | 977 | if (!det_stats_length(rc, param->filename)){ |
632 : | suxen_drol | 1032 | DPRINTF(XVID_DEBUG_RC,"fopen %s failed\n", param->filename); |
633 : | suxen_drol | 942 | free(rc); |
634 : | return XVID_ERR_FAIL; | ||
635 : | } | ||
636 : | |||
637 : | if ((rc->stats = malloc(rc->num_frames * sizeof(stat_t))) == NULL) { | ||
638 : | free(rc); | ||
639 : | return XVID_ERR_MEMORY; | ||
640 : | } | ||
641 : | |||
642 : | /* XXX: do we need an addition location */ | ||
643 : | if ((rc->keyframe_locations = malloc((rc->num_keyframes + 1) * sizeof(int))) == NULL) { | ||
644 : | free(rc->stats); | ||
645 : | free(rc); | ||
646 : | return XVID_ERR_MEMORY; | ||
647 : | } | ||
648 : | |||
649 : | suxen_drol | 977 | if (!load_stats(rc, param->filename)) { |
650 : | suxen_drol | 1032 | DPRINTF(XVID_DEBUG_RC,"fopen %s failed\n", param->filename); |
651 : | suxen_drol | 942 | free(rc->keyframe_locations); |
652 : | free(rc->stats); | ||
653 : | free(rc); | ||
654 : | return XVID_ERR_FAIL; | ||
655 : | } | ||
656 : | |||
657 : | /* pre-process our stats */ | ||
658 : | suxen_drol | 977 | |
659 : | suxen_drol | 1030 | if (rc->num_frames < create->fbase/create->fincr) { |
660 : | rc->target = rc->param.bitrate / 8; /* one second */ | ||
661 : | }else{ | ||
662 : | rc->target = (rc->param.bitrate * rc->num_frames * create->fincr) / (create->fbase * 8); | ||
663 : | suxen_drol | 977 | } |
664 : | suxen_drol | 1030 | |
665 : | suxen_drol | 1032 | DPRINTF(XVID_DEBUG_RC, "rc->target : %i\n", rc->target); |
666 : | |||
667 : | suxen_drol | 1030 | rc->target -= rc->num_frames*24; /* avi file header */ |
668 : | suxen_drol | 942 | |
669 : | suxen_drol | 977 | |
670 : | pre_process0(rc); | ||
671 : | if (rc->param.bitrate) { | ||
672 : | suxen_drol | 1030 | zone_process(rc, create); |
673 : | suxen_drol | 977 | internal_scale(rc); |
674 : | suxen_drol | 1030 | }else{ |
675 : | /* external scaler: ignore zone */ | ||
676 : | for (i=0;i<rc->num_frames;i++) { | ||
677 : | rc->stats[i].zone_mode = XVID_ZONE_WEIGHT; | ||
678 : | rc->stats[i].weight = 1.0; | ||
679 : | } | ||
680 : | rc->avg_weight = 1.0; | ||
681 : | rc->tot_quant = 0; | ||
682 : | } | ||
683 : | pre_process1(rc); | ||
684 : | |||
685 : | for (i=0; i<32;i++) { | ||
686 : | rc->pquant_error[i] = 0; | ||
687 : | rc->bquant_error[i] = 0; | ||
688 : | rc->quant_count[i] = 0; | ||
689 : | } | ||
690 : | |||
691 : | rc->fq_error = 0; | ||
692 : | suxen_drol | 977 | |
693 : | suxen_drol | 942 | *handle = rc; |
694 : | return(0); | ||
695 : | } | ||
696 : | |||
697 : | |||
698 : | static int rc_2pass2_destroy(rc_2pass2_t * rc, xvid_plg_destroy_t * destroy) | ||
699 : | { | ||
700 : | free(rc->keyframe_locations); | ||
701 : | free(rc->stats); | ||
702 : | free(rc); | ||
703 : | return(0); | ||
704 : | } | ||
705 : | |||
706 : | |||
707 : | |||
708 : | static int rc_2pass2_before(rc_2pass2_t * rc, xvid_plg_data_t * data) | ||
709 : | { | ||
710 : | stat_t * s = &rc->stats[data->frame_num]; | ||
711 : | int overflow; | ||
712 : | int desired; | ||
713 : | double dbytes; | ||
714 : | double curve_temp; | ||
715 : | int capped_to_max_framesize = 0; | ||
716 : | |||
717 : | suxen_drol | 1030 | if (data->quant <= 0) { |
718 : | suxen_drol | 942 | |
719 : | suxen_drol | 1030 | if (s->zone_mode == XVID_ZONE_QUANT) { |
720 : | suxen_drol | 942 | |
721 : | suxen_drol | 1030 | rc->fq_error += s->weight; |
722 : | data->quant = (int)rc->fq_error; | ||
723 : | rc->fq_error -= data->quant; | ||
724 : | suxen_drol | 942 | |
725 : | suxen_drol | 1032 | s->desired_length = s->length; |
726 : | |||
727 : | suxen_drol | 1030 | }else { /* XVID_ZONE_WEIGHT */ |
728 : | suxen_drol | 942 | |
729 : | suxen_drol | 1030 | if (data->frame_num >= rc->num_frames) { |
730 : | /* insufficent stats data */ | ||
731 : | return 0; | ||
732 : | } | ||
733 : | suxen_drol | 977 | |
734 : | suxen_drol | 1030 | overflow = rc->overflow / 8; /* XXX: why by 8 */ |
735 : | suxen_drol | 942 | |
736 : | suxen_drol | 1030 | if (s->type == XVID_TYPE_IVOP) { /* XXX: why */ |
737 : | overflow = 0; | ||
738 : | } | ||
739 : | suxen_drol | 942 | |
740 : | suxen_drol | 1030 | desired = s->scaled_length; |
741 : | suxen_drol | 942 | |
742 : | suxen_drol | 1030 | dbytes = desired; |
743 : | if (s->type == XVID_TYPE_IVOP) { | ||
744 : | dbytes += desired * rc->param.keyframe_boost / 100; | ||
745 : | } | ||
746 : | dbytes /= rc->movie_curve; | ||
747 : | suxen_drol | 942 | |
748 : | suxen_drol | 1030 | if (s->type == XVID_TYPE_BVOP) { |
749 : | dbytes *= rc->avg_length[XVID_TYPE_PVOP-1] / rc->avg_length[XVID_TYPE_BVOP-1]; | ||
750 : | } | ||
751 : | suxen_drol | 942 | |
752 : | suxen_drol | 1030 | if (rc->param.payback_method == XVID_PAYBACK_BIAS) { |
753 : | desired =(int)(rc->curve_comp_error / rc->param.bitrate_payback_delay); | ||
754 : | }else{ | ||
755 : | //printf("desired=%i, dbytes=%i\n", desired,dbytes); | ||
756 : | desired = (int)(rc->curve_comp_error * dbytes / | ||
757 : | rc->avg_length[XVID_TYPE_PVOP-1] / rc->param.bitrate_payback_delay); | ||
758 : | //printf("desired=%i\n", desired); | ||
759 : | suxen_drol | 942 | |
760 : | suxen_drol | 1030 | if (labs(desired) > fabs(rc->curve_comp_error)) { |
761 : | desired = (int)rc->curve_comp_error; | ||
762 : | } | ||
763 : | } | ||
764 : | suxen_drol | 942 | |
765 : | suxen_drol | 1030 | rc->curve_comp_error -= desired; |
766 : | suxen_drol | 942 | |
767 : | suxen_drol | 1030 | /* alt curve */ |
768 : | suxen_drol | 942 | |
769 : | suxen_drol | 1030 | curve_temp = 0; /* XXX: warning */ |
770 : | suxen_drol | 942 | |
771 : | suxen_drol | 1030 | if (rc->param.use_alt_curve) { |
772 : | if (s->type != XVID_TYPE_IVOP) { | ||
773 : | if (dbytes > rc->avg_length[XVID_TYPE_PVOP-1]) { | ||
774 : | if (dbytes >= rc->alt_curve_high) { | ||
775 : | curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev); | ||
776 : | }else{ | ||
777 : | switch(rc->param.alt_curve_type) { | ||
778 : | case XVID_CURVE_SINE : | ||
779 : | curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * sin(DEG2RAD * ((dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) * 90.0 / rc->alt_curve_high_diff))); | ||
780 : | break; | ||
781 : | case XVID_CURVE_LINEAR : | ||
782 : | curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * (dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) / rc->alt_curve_high_diff); | ||
783 : | break; | ||
784 : | case XVID_CURVE_COSINE : | ||
785 : | curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * (1.0 - cos(DEG2RAD * ((dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) * 90.0 / rc->alt_curve_high_diff)))); | ||
786 : | } | ||
787 : | } | ||
788 : | }else{ | ||
789 : | if (dbytes <= rc->alt_curve_low){ | ||
790 : | curve_temp = dbytes; | ||
791 : | }else{ | ||
792 : | switch(rc->param.alt_curve_type) { | ||
793 : | case XVID_CURVE_SINE : | ||
794 : | curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * sin(DEG2RAD * ((dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) * 90.0 / rc->alt_curve_low_diff))); | ||
795 : | break; | ||
796 : | case XVID_CURVE_LINEAR : | ||
797 : | curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev * (dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) / rc->alt_curve_low_diff); | ||
798 : | break; | ||
799 : | case XVID_CURVE_COSINE : | ||
800 : | curve_temp = dbytes * (rc->alt_curve_mid_qual + rc->alt_curve_qual_dev * (1.0 - cos(DEG2RAD * ((dbytes - rc->avg_length[XVID_TYPE_PVOP-1]) * 90.0 / rc->alt_curve_low_diff)))); | ||
801 : | } | ||
802 : | } | ||
803 : | } | ||
804 : | if (s->type == XVID_TYPE_BVOP) | ||
805 : | curve_temp *= rc->avg_length[XVID_TYPE_BVOP-1] / rc->avg_length[XVID_TYPE_PVOP-1]; | ||
806 : | suxen_drol | 942 | |
807 : | suxen_drol | 1030 | curve_temp = curve_temp * rc->curve_comp_scale + rc->alt_curve_curve_bias_bonus; |
808 : | suxen_drol | 942 | |
809 : | suxen_drol | 1030 | desired += ((int)curve_temp); |
810 : | rc->curve_comp_error += curve_temp - (int)curve_temp; | ||
811 : | }else{ | ||
812 : | if (s->type == XVID_TYPE_BVOP) | ||
813 : | dbytes *= rc->avg_length[XVID_TYPE_BVOP-1] / rc->avg_length[XVID_TYPE_PVOP-1]; | ||
814 : | suxen_drol | 942 | |
815 : | suxen_drol | 1030 | desired += ((int)dbytes); |
816 : | rc->curve_comp_error += dbytes - (int)dbytes; | ||
817 : | } | ||
818 : | suxen_drol | 942 | |
819 : | suxen_drol | 1030 | }else if ((rc->param.curve_compression_high + rc->param.curve_compression_low) && s->type != XVID_TYPE_IVOP) { |
820 : | suxen_drol | 942 | |
821 : | suxen_drol | 1030 | curve_temp = rc->curve_comp_scale; |
822 : | if (dbytes > rc->avg_length[XVID_TYPE_PVOP-1]) { | ||
823 : | curve_temp *= ((double)dbytes + (rc->avg_length[XVID_TYPE_PVOP-1] - dbytes) * rc->param.curve_compression_high / 100.0); | ||
824 : | } else { | ||
825 : | curve_temp *= ((double)dbytes + (rc->avg_length[XVID_TYPE_PVOP-1] - dbytes) * rc->param.curve_compression_low / 100.0); | ||
826 : | } | ||
827 : | |||
828 : | if (s->type == XVID_TYPE_BVOP){ | ||
829 : | curve_temp *= rc->avg_length[XVID_TYPE_BVOP-1] / rc->avg_length[XVID_TYPE_PVOP-1]; | ||
830 : | } | ||
831 : | |||
832 : | desired += (int)curve_temp; | ||
833 : | rc->curve_comp_error += curve_temp - (int)curve_temp; | ||
834 : | }else{ | ||
835 : | if (s->type == XVID_TYPE_BVOP){ | ||
836 : | dbytes *= rc->avg_length[XVID_TYPE_BVOP-1] / rc->avg_length[XVID_TYPE_PVOP-1]; | ||
837 : | } | ||
838 : | |||
839 : | desired += (int)dbytes; | ||
840 : | rc->curve_comp_error += dbytes - (int)dbytes; | ||
841 : | suxen_drol | 942 | } |
842 : | |||
843 : | suxen_drol | 1032 | |
844 : | if (desired > s->length) { /* if desired length exceeds the pass1 length.. */ | ||
845 : | suxen_drol | 1030 | rc->curve_comp_error += desired - s->length; |
846 : | desired = s->length; | ||
847 : | }else{ | ||
848 : | if (desired < rc->min_length[s->type-1]) { | ||
849 : | if (s->type == XVID_TYPE_IVOP){ | ||
850 : | rc->curve_comp_error -= rc->min_length[XVID_TYPE_IVOP-1] - desired; | ||
851 : | } | ||
852 : | desired = rc->min_length[s->type-1]; | ||
853 : | } | ||
854 : | } | ||
855 : | suxen_drol | 942 | |
856 : | suxen_drol | 1030 | s->desired_length = desired; |
857 : | suxen_drol | 942 | |
858 : | suxen_drol | 1030 | |
859 : | /* if this keyframe is too close to the next, reduce it's byte allotment | ||
860 : | XXX: why do we do this after setting the desired length */ | ||
861 : | suxen_drol | 942 | |
862 : | suxen_drol | 1030 | if (s->type == XVID_TYPE_IVOP) { |
863 : | int KFdistance = rc->keyframe_locations[rc->KF_idx] - rc->keyframe_locations[rc->KF_idx - 1]; | ||
864 : | suxen_drol | 942 | |
865 : | suxen_drol | 1030 | if (KFdistance < rc->param.kftreshold) { |
866 : | |||
867 : | KFdistance = KFdistance - rc->param.min_key_interval; | ||
868 : | suxen_drol | 942 | |
869 : | suxen_drol | 1030 | if (KFdistance >= 0) { |
870 : | int KF_min_size; | ||
871 : | suxen_drol | 942 | |
872 : | suxen_drol | 1030 | KF_min_size = desired * (100 - rc->param.kfreduction) / 100; |
873 : | if (KF_min_size < 1) | ||
874 : | KF_min_size = 1; | ||
875 : | suxen_drol | 942 | |
876 : | suxen_drol | 1030 | desired = KF_min_size + (desired - KF_min_size) * KFdistance / |
877 : | (rc->param.kftreshold - rc->param.min_key_interval); | ||
878 : | suxen_drol | 942 | |
879 : | suxen_drol | 1030 | if (desired < 1) |
880 : | desired = 1; | ||
881 : | } | ||
882 : | } | ||
883 : | } | ||
884 : | suxen_drol | 942 | |
885 : | suxen_drol | 1030 | overflow = (int)((double)overflow * desired / rc->avg_length[XVID_TYPE_PVOP-1]); |
886 : | suxen_drol | 942 | |
887 : | suxen_drol | 1030 | // Foxer: reign in overflow with huge frames |
888 : | if (labs(overflow) > labs(rc->overflow)) { | ||
889 : | overflow = rc->overflow; | ||
890 : | } | ||
891 : | suxen_drol | 942 | |
892 : | suxen_drol | 1030 | // Foxer: make sure overflow doesn't run away |
893 : | suxen_drol | 942 | |
894 : | suxen_drol | 1030 | if (overflow > desired * rc->param.max_overflow_improvement / 100) { |
895 : | desired += (overflow <= desired) ? desired * rc->param.max_overflow_improvement / 100 : | ||
896 : | overflow * rc->param.max_overflow_improvement / 100; | ||
897 : | }else if (overflow < desired * rc->param.max_overflow_degradation / -100){ | ||
898 : | desired += desired * rc->param.max_overflow_degradation / -100; | ||
899 : | }else{ | ||
900 : | desired += overflow; | ||
901 : | } | ||
902 : | suxen_drol | 942 | |
903 : | suxen_drol | 1030 | if (desired > rc->max_length) { |
904 : | capped_to_max_framesize = 1; | ||
905 : | desired = rc->max_length; | ||
906 : | } | ||
907 : | suxen_drol | 942 | |
908 : | suxen_drol | 1030 | // make sure to not scale below the minimum framesize |
909 : | if (desired < rc->min_length[s->type-1]) { | ||
910 : | desired = rc->min_length[s->type-1]; | ||
911 : | } | ||
912 : | suxen_drol | 942 | |
913 : | |||
914 : | suxen_drol | 1030 | // very 'simple' quant<->filesize relationship |
915 : | data->quant= (s->quant * s->length) / desired; | ||
916 : | suxen_drol | 942 | |
917 : | suxen_drol | 1030 | if (data->quant < 1) { |
918 : | data->quant = 1; | ||
919 : | } else if (data->quant > 31) { | ||
920 : | data->quant = 31; | ||
921 : | } | ||
922 : | else if (s->type != XVID_TYPE_IVOP) | ||
923 : | { | ||
924 : | // Foxer: aid desired quantizer precision by accumulating decision error | ||
925 : | if (s->type== XVID_TYPE_BVOP) { | ||
926 : | rc->bquant_error[data->quant] += ((double)(s->quant * s->length) / desired) - data->quant; | ||
927 : | suxen_drol | 942 | |
928 : | suxen_drol | 1030 | if (rc->bquant_error[data->quant] >= 1.0) { |
929 : | rc->bquant_error[data->quant] -= 1.0; | ||
930 : | data->quant++; | ||
931 : | } | ||
932 : | }else{ | ||
933 : | rc->pquant_error[data->quant] += ((double)(s->quant * s->length) / desired) - data->quant; | ||
934 : | suxen_drol | 942 | |
935 : | suxen_drol | 1030 | if (rc->pquant_error[data->quant] >= 1.0) { |
936 : | rc->pquant_error[data->quant] -= 1.0; | ||
937 : | ++data->quant; | ||
938 : | } | ||
939 : | } | ||
940 : | } | ||
941 : | suxen_drol | 942 | |
942 : | suxen_drol | 1030 | /* cap to min/max quant */ |
943 : | suxen_drol | 942 | |
944 : | suxen_drol | 1030 | if (data->quant < data->min_quant[s->type-1]) { |
945 : | data->quant = data->min_quant[s->type-1]; | ||
946 : | }else if (data->quant > data->max_quant[s->type-1]) { | ||
947 : | data->quant = data->max_quant[s->type-1]; | ||
948 : | } | ||
949 : | suxen_drol | 942 | |
950 : | suxen_drol | 1030 | /* subsequent p/b frame quants can only be +- 2 */ |
951 : | if (s->type != XVID_TYPE_IVOP && rc->last_quant[s->type-1] && capped_to_max_framesize == 0) { | ||
952 : | suxen_drol | 942 | |
953 : | suxen_drol | 1030 | if (data->quant > rc->last_quant[s->type-1] + 2) { |
954 : | data->quant = rc->last_quant[s->type-1] + 2; | ||
955 : | suxen_drol | 1032 | DPRINTF(XVID_DEBUG_RC, "p/b-frame quantizer prevented from rising too steeply"); |
956 : | suxen_drol | 1030 | } |
957 : | if (data->quant < rc->last_quant[s->type-1] - 2) { | ||
958 : | data->quant = rc->last_quant[s->type-1] - 2; | ||
959 : | suxen_drol | 1032 | DPRINTF(XVID_DEBUG_RC, "p/b-frame quantizer prevented from falling too steeply"); |
960 : | suxen_drol | 1030 | } |
961 : | } | ||
962 : | suxen_drol | 942 | |
963 : | suxen_drol | 1030 | if (capped_to_max_framesize == 0) { |
964 : | rc->last_quant[s->type-1] = data->quant; | ||
965 : | } | ||
966 : | |||
967 : | |||
968 : | } /* if */ | ||
969 : | |||
970 : | } | ||
971 : | |||
972 : | return 0; | ||
973 : | suxen_drol | 942 | } |
974 : | |||
975 : | |||
976 : | |||
977 : | static int rc_2pass2_after(rc_2pass2_t * rc, xvid_plg_data_t * data) | ||
978 : | { | ||
979 : | stat_t * s = &rc->stats[data->frame_num]; | ||
980 : | |||
981 : | if (data->frame_num >= rc->num_frames) { | ||
982 : | /* insufficent stats data */ | ||
983 : | return 0; | ||
984 : | } | ||
985 : | |||
986 : | rc->quant_count[data->quant]++; | ||
987 : | |||
988 : | if (data->type == XVID_TYPE_IVOP) { | ||
989 : | int kfdiff = (rc->keyframe_locations[rc->KF_idx] - rc->keyframe_locations[rc->KF_idx - 1]); | ||
990 : | |||
991 : | rc->overflow += rc->KFoverflow; | ||
992 : | rc->KFoverflow = s->desired_length - data->length; | ||
993 : | |||
994 : | if (kfdiff > 1) { // non-consecutive keyframes | ||
995 : | rc->KFoverflow_partial = rc->KFoverflow / (kfdiff - 1); | ||
996 : | }else{ // consecutive keyframes | ||
997 : | rc->overflow += rc->KFoverflow; | ||
998 : | rc->KFoverflow = 0; | ||
999 : | rc->KFoverflow_partial = 0; | ||
1000 : | } | ||
1001 : | rc->KF_idx++; | ||
1002 : | }else{ | ||
1003 : | // distribute part of the keyframe overflow | ||
1004 : | rc->overflow += s->desired_length - data->length + rc->KFoverflow_partial; | ||
1005 : | rc->KFoverflow -= rc->KFoverflow_partial; | ||
1006 : | } | ||
1007 : | |||
1008 : | suxen_drol | 1032 | DPRINTF(XVID_DEBUG_RC, "[%i] quant:%i stats1:%i scaled:%i actual:%i overflow:%i\n", |
1009 : | suxen_drol | 942 | data->frame_num, |
1010 : | data->quant, | ||
1011 : | s->length, | ||
1012 : | s->scaled_length, | ||
1013 : | data->length, | ||
1014 : | rc->overflow); | ||
1015 : | |||
1016 : | return(0); | ||
1017 : | } | ||
1018 : | |||
1019 : | |||
1020 : | |||
1021 : | int xvid_plugin_2pass2(void * handle, int opt, void * param1, void * param2) | ||
1022 : | { | ||
1023 : | switch(opt) | ||
1024 : | { | ||
1025 : | case XVID_PLG_INFO : | ||
1026 : | return 0; | ||
1027 : | |||
1028 : | case XVID_PLG_CREATE : | ||
1029 : | return rc_2pass2_create((xvid_plg_create_t*)param1, param2); | ||
1030 : | |||
1031 : | case XVID_PLG_DESTROY : | ||
1032 : | return rc_2pass2_destroy((rc_2pass2_t*)handle, (xvid_plg_destroy_t*)param1); | ||
1033 : | |||
1034 : | case XVID_PLG_BEFORE : | ||
1035 : | return rc_2pass2_before((rc_2pass2_t*)handle, (xvid_plg_data_t*)param1); | ||
1036 : | |||
1037 : | case XVID_PLG_AFTER : | ||
1038 : | return rc_2pass2_after((rc_2pass2_t*)handle, (xvid_plg_data_t*)param1); | ||
1039 : | } | ||
1040 : | |||
1041 : | return XVID_ERR_FAIL; | ||
1042 : | } |
No admin address has been configured | ViewVC Help |
Powered by ViewVC 1.0.4 |