[cvs] / xvidcore / src / plugins / plugin_2pass2.c Repository:
ViewVC logotype

Diff of /xvidcore/src/plugins/plugin_2pass2.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1, Tue Mar 25 10:58:33 2003 UTC revision 1.1.2.4, Fri May 16 17:19:51 2003 UTC
# Line 0  Line 1 
1    /******************************************************************************
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     * $Id$
26     *
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            int blks[3];                    /* k,m,y blks */
42        int length;             /* first pass length */
43        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    } 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            int num_frames;
60        int num_keyframes;
61        uint64_t target;    /* target filesize */
62    
63        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    
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        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    
102        double fq_error;
103    } 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        while((n = fscanf(f, "%c %d %d %d %d %d %d\n",
126            &type, &ignore, &ignore, &ignore, &ignore, &ignore, &ignore)) != EOF) {
127            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    /* open stats file(s) and read into rc->stats array */
143    
144    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                printf("unk\n");
175                continue;
176            }
177    
178            i++;
179        }
180        rc->num_frames = i;
181    
182        fclose(f);
183    
184        return 1;
185    }
186    
187    
188    
189    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            printf("%i %i %i %i\n", s->type, s->quant, s->length, s->scaled_length);
195    
196        }
197    }
198    
199    
200    /* pre-process the statistics data
201        - for each type, count, tot_length, min_length, max_length
202        - set keyframes_locations
203    */
204    
205    void pre_process0(rc_2pass2_t * rc)
206    {
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    }
236    
237    
238    /* calculate zone weight "center" */
239    
240    static void zone_process(rc_2pass2_t *rc, const xvid_plg_create_t * create)
241    {
242        int i,j;
243        int n = 0;
244    
245        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        /*for (i=0; i < rc->num_frames; i++) {
291            printf("[%i] mode=%i weight=%f\n", i, rc->stats[i].zone_mode, rc->stats[i].weight);
292        }*/
293    
294        printf("center_weight: %f (for %i frames);   fixed_bytes: %i\n", rc->avg_weight, n, rc->tot_quant);
295    }
296    
297    
298    /* scale the curve */
299    
300    static void internal_scale(rc_2pass2_t *rc)
301    {
302            int64_t target  = rc->target - rc->tot_quant;
303            int64_t pass1_length = rc->tot_length[0] + rc->tot_length[1] + rc->tot_length[2] - rc->tot_quant;
304            int min_size[3];
305            double scaler;
306            int i;
307    
308    
309            if (target <= 0 || target >= pass1_length) {
310                    printf("undersize warning\n");
311            }
312    
313            /* perform an initial scale pass.
314               if a frame size is scaled underneath our hardcoded minimums, then we force the
315               frame size to the minimum, and deduct the original & scaled frmae length from the
316               original and target total lengths */
317    
318            min_size[0] = ((rc->stats[0].blks[0]*22) + 240) / 8;
319            min_size[1] = (rc->stats[0].blks[0] + 88) / 8;
320            min_size[2] = 8;
321    
322    
323            scaler = (double)target / (double)pass1_length;
324            //printf("target=%i, tot_length=%i, scaler=%f\n", (int)target, (int)pass1_length, scaler);
325    
326            for (i=0; i<rc->num_frames; i++) {
327                    stat_t * s = &rc->stats[i];
328                    int len;
329    
330            if (s->zone_mode == XVID_ZONE_QUANT) {
331                s->scaled_length = s->length;
332            }else {
333                        len = (int)((double)s->length * scaler * s->weight / rc->avg_weight);
334                        if (len < min_size[s->type]) {              /* force frame size */
335                                s->scaled_length = min_size[s->type];
336                                target -= s->scaled_length;
337                                pass1_length -= s->length;
338                        }else{
339                                s->scaled_length = 0;
340                        }
341            }
342            }
343    
344            if (target <= 0 || target >= pass1_length) {
345                    printf("undersize warning\n");
346                    return;
347            }
348    
349            scaler = (double)target / (double)pass1_length;
350            //printf("target=%i, tot_length=%i, scaler=%f\n", (int)target, (int)tot_length, scaler);
351    
352            for (i=0; i<rc->num_frames; i++) {
353                    stat_t * s = &rc->stats[i];
354    
355                    if (s->scaled_length==0) {      /* ignore frame with forced frame sizes */
356                            s->scaled_length = (int)((double)s->length * scaler * s->weight / rc->avg_weight);
357                    }
358            }
359    
360    }
361    
362    
363    
364    
365    void pre_process1(rc_2pass2_t * rc)
366    {
367        int i;
368        double total1, total2;
369        uint64_t ivop_boost_total;
370    
371        ivop_boost_total = 0;
372        rc->curve_comp_error = 0;
373    
374        for (i=0; i<3; i++) {
375            rc->tot_scaled_length[i] = 0;
376        }
377    
378        for (i=0; i<rc->num_frames; i++) {
379            stat_t * s = &rc->stats[i];
380    
381            rc->tot_scaled_length[s->type-1] += s->scaled_length;
382    
383            if (s->type == XVID_TYPE_IVOP) {
384                ivop_boost_total += s->scaled_length * rc->param.keyframe_boost / 100;
385            }
386        }
387    
388        rc->movie_curve = ((double)(rc->tot_scaled_length[XVID_TYPE_PVOP-1] + rc->tot_scaled_length[XVID_TYPE_BVOP-1] + ivop_boost_total) /
389                                            (rc->tot_scaled_length[XVID_TYPE_PVOP-1] + rc->tot_scaled_length[XVID_TYPE_BVOP-1]));
390    
391        for(i=0; i<3; i++) {
392            if (rc->count[i] == 0 || rc->movie_curve == 0) {
393                rc->avg_length[i] = 1;
394            }else{
395                rc->avg_length[i] = rc->tot_scaled_length[i] / rc->count[i] / rc->movie_curve;
396            }
397        }
398    
399        /* alt curve stuff here */
400    
401        if (rc->param.use_alt_curve) {
402            const double avg_pvop = rc->avg_length[XVID_TYPE_PVOP-1];
403            const uint64_t tot_pvop = rc->tot_length[XVID_TYPE_PVOP-1];
404            const uint64_t tot_bvop = rc->tot_length[XVID_TYPE_BVOP-1];
405            const uint64_t tot_scaled_pvop = rc->tot_scaled_length[XVID_TYPE_PVOP-1];
406            const uint64_t tot_scaled_bvop = rc->tot_scaled_length[XVID_TYPE_BVOP-1];
407    
408                    rc->alt_curve_low = avg_pvop - avg_pvop * (double)rc->param.alt_curve_low_dist / 100.0;
409                    rc->alt_curve_low_diff = avg_pvop - rc->alt_curve_low;
410                    rc->alt_curve_high = avg_pvop + avg_pvop * (double)rc->param.alt_curve_high_dist / 100.0;
411                    rc->alt_curve_high_diff = rc->alt_curve_high - avg_pvop;
412    
413            if (rc->param.alt_curve_use_auto) {
414                if (tot_bvop + tot_pvop > tot_scaled_bvop + tot_scaled_pvop) {
415                                    rc->param.alt_curve_min_rel_qual = (int)(100.0 - (100.0 - 100.0 /
416                                            ((double)(tot_pvop + tot_bvop) / (double)(tot_scaled_pvop + tot_scaled_bvop))) * (double)rc->param.alt_curve_auto_str / 100.0);
417    
418                                    if (rc->param.alt_curve_min_rel_qual < 20)
419                                            rc->param.alt_curve_min_rel_qual = 20;
420                }else{
421                                    rc->param.alt_curve_min_rel_qual = 100;
422                }
423            }
424                    rc->alt_curve_mid_qual = (1.0 + (double)rc->param.alt_curve_min_rel_qual / 100.0) / 2.0;
425                    rc->alt_curve_qual_dev = 1.0 - rc->alt_curve_mid_qual;
426    
427            if (rc->param.alt_curve_low_dist > 100) {
428                            switch(rc->param.alt_curve_type) {
429                case XVID_CURVE_SINE: // Sine Curve (high aggressiveness)
430                                    rc->alt_curve_qual_dev *= 2.0 / (1.0 + sin(DEG2RAD * (avg_pvop * 90.0 / rc->alt_curve_low_diff)));
431                                    rc->alt_curve_mid_qual = 1.0 - rc->alt_curve_qual_dev * sin(DEG2RAD * (avg_pvop * 90.0 / rc->alt_curve_low_diff));
432                                    break;
433                            case XVID_CURVE_LINEAR: // Linear (medium aggressiveness)
434                                    rc->alt_curve_qual_dev *= 2.0 / (1.0 + avg_pvop / rc->alt_curve_low_diff);
435                                    rc->alt_curve_mid_qual = 1.0 - rc->alt_curve_qual_dev * avg_pvop / rc->alt_curve_low_diff;
436                                    break;
437                            case XVID_CURVE_COSINE: // Cosine Curve (low aggressiveness)
438                                    rc->alt_curve_qual_dev *= 2.0 / (1.0 + (1.0 - cos(DEG2RAD * (avg_pvop * 90.0 / rc->alt_curve_low_diff))));
439                                    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)));
440                            }
441                    }
442        }
443        /* --- */
444    
445    
446        total1=total2=0;
447        for (i=0; i<rc->num_frames; i++) {
448            stat_t * s = &rc->stats[i];
449    
450            if (s->type != XVID_TYPE_IVOP) {
451                double dbytes,dbytes2;
452    
453                dbytes = s->scaled_length / rc->movie_curve;
454                dbytes2 = 0; /* XXX: warning */
455                total1 += dbytes;
456                if (s->type == XVID_TYPE_BVOP)
457                    dbytes *= rc->avg_length[XVID_TYPE_PVOP-1] / rc->avg_length[XVID_TYPE_BVOP-1];
458    
459                if (rc->param.use_alt_curve) {
460                    if (dbytes > rc->avg_length[XVID_TYPE_PVOP-1]) {
461    
462                        if (dbytes >= rc->alt_curve_high) {
463                                                    dbytes2 = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev);
464                        }else{
465                                                    switch(rc->param.alt_curve_type) {
466                            case XVID_CURVE_SINE :
467                                                        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)));
468                                                            break;
469                            case XVID_CURVE_LINEAR :
470                                                        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);
471                                                            break;
472                                                    case XVID_CURVE_COSINE :
473                                                        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))));
474                                                    }
475                                            }
476                    }else{
477                        if (dbytes <= rc->alt_curve_low) {
478                                                    dbytes2 = dbytes;
479                        }else{
480                                                    switch(rc->param.alt_curve_type) {
481                                                    case XVID_CURVE_SINE :
482                                                        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)));
483                                                            break;
484                                                    case XVID_CURVE_LINEAR :
485                                                        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);
486                                                            break;
487                                                    case XVID_CURVE_COSINE :
488                                                        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))));
489                                                    }
490                                            }
491    
492                    }
493    
494    
495                }else{
496                    if (dbytes > rc->avg_length[XVID_TYPE_PVOP-1]) {
497                        dbytes2=((double)dbytes + (rc->avg_length[XVID_TYPE_PVOP-1] - dbytes) * rc->param.curve_compression_high / 100.0);
498                    }else{
499                                    dbytes2 = ((double)dbytes + (rc->avg_length[XVID_TYPE_PVOP-1] - dbytes) * rc->param.curve_compression_low / 100.0);
500                    }
501                }
502    
503                if (s->type == XVID_TYPE_BVOP) {
504                                dbytes2 *= rc->avg_length[XVID_TYPE_BVOP-1] / rc->avg_length[XVID_TYPE_PVOP-1];
505                                if (dbytes2 < rc->min_length[XVID_TYPE_BVOP-1])
506                                        dbytes2 = rc->min_length[XVID_TYPE_BVOP-1];
507                }else{
508                                if (dbytes2 < rc->min_length[XVID_TYPE_PVOP-1])
509                                        dbytes2 = rc->min_length[XVID_TYPE_PVOP-1];
510                }
511                total2 += dbytes2;
512            }
513        }
514    
515        rc->curve_comp_scale = total1 / total2;
516    
517        if (!rc->param.use_alt_curve) {
518            printf("middle frame size for asymmetric curve compression: %i\n",
519                (int)(rc->avg_length[XVID_TYPE_PVOP-1] * rc->curve_comp_scale));
520        }
521    
522        if (rc->param.use_alt_curve) {
523            int bonus_bias = rc->param.alt_curve_bonus_bias;
524            int oldquant = 1;
525    
526                if (rc->param.alt_curve_use_auto_bonus_bias)
527                        bonus_bias = rc->param.alt_curve_min_rel_qual;
528    
529                rc->alt_curve_curve_bias_bonus = (total1 - total2) * (double)bonus_bias / 100.0 / (double)(rc->num_frames /* - credits_frames */ - rc->num_keyframes);
530                rc->curve_comp_scale = ((total1 - total2) * (1.0 - (double)bonus_bias / 100.0) + total2) / total2;
531    
532    
533            /* special info for alt curve:  bias bonus and quantizer thresholds */
534    
535                    printf("avg scaled framesize:%i", (int)rc->avg_length[XVID_TYPE_PVOP-1]);
536                    printf("bias bonus:%i bytes", (int)rc->alt_curve_curve_bias_bonus);
537    
538                    for (i=1; i <= (int)(rc->alt_curve_high*2)+1; i++) {
539                double curve_temp, dbytes;
540                int newquant;
541    
542                dbytes = i;
543                            if (dbytes > rc->avg_length[XVID_TYPE_PVOP-1]) {
544                    if (dbytes >= rc->alt_curve_high) {
545                                            curve_temp = dbytes * (rc->alt_curve_mid_qual - rc->alt_curve_qual_dev);
546                    }else{
547                                            switch(rc->param.alt_curve_type)
548                                            {
549                                            case XVID_CURVE_SINE :
550                                                    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)));
551                                                    break;
552                                            case XVID_CURVE_LINEAR :
553                                                    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);
554                                                    break;
555                                            case XVID_CURVE_COSINE :
556                                                    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))));
557                                            }
558                                    }
559                            }else{
560                    if (dbytes <= rc->alt_curve_low) {
561                                            curve_temp = dbytes;
562                    }else{
563                                            switch(rc->param.alt_curve_type)
564                                            {
565                                            case XVID_CURVE_SINE :
566                                                    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)));
567                                                    break;
568                                            case XVID_CURVE_LINEAR :
569                                                    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);
570                                                    break;
571                                            case XVID_CURVE_COSINE :
572                                                    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))));
573                                            }
574                                    }
575                            }
576    
577                            if (rc->movie_curve > 1.0)
578                                    dbytes *= rc->movie_curve;
579    
580                            newquant = (int)(dbytes * 2.0 / (curve_temp * rc->curve_comp_scale + rc->alt_curve_curve_bias_bonus));
581                            if (newquant > 1) {
582                                    if (newquant != oldquant) {
583                        int percent = (int)((i - rc->avg_length[XVID_TYPE_PVOP-1]) * 100.0 / rc->avg_length[XVID_TYPE_PVOP-1]);
584                                            oldquant = newquant;
585                                            printf("quant:%i threshold at %i : %i percent", newquant, i, percent);
586                                    }
587                            }
588                    }
589    
590        }
591    
592        rc->overflow = 0;
593        rc->KFoverflow = 0;
594        rc->KFoverflow_partial = 0;
595        rc->KF_idx = 1;
596    }
597    
598    
599    
600    
601    static int rc_2pass2_create(xvid_plg_create_t * create, rc_2pass2_t ** handle)
602    {
603        xvid_plugin_2pass2_t * param = (xvid_plugin_2pass2_t *)create->param;
604        rc_2pass2_t * rc;
605        int i;
606    
607        rc = malloc(sizeof(rc_2pass2_t));
608        if (rc == NULL)
609            return XVID_ERR_MEMORY;
610    
611        rc->param = *param;
612    
613        if (rc->param.keyframe_boost <= 0) rc->param.keyframe_boost = 0;
614        if (rc->param.payback_method <= 0) rc->param.payback_method = XVID_PAYBACK_PROP;
615        if (rc->param.bitrate_payback_delay <= 0) rc->param.bitrate_payback_delay = 250;
616        if (rc->param.curve_compression_high <= 0) rc->param.curve_compression_high = 0;
617        if (rc->param.curve_compression_low <= 0) rc->param.curve_compression_low = 0;
618        if (rc->param.max_overflow_improvement <= 0) rc->param.max_overflow_improvement = 60;
619        if (rc->param.max_overflow_degradation <= 0) rc->param.max_overflow_degradation = 60;
620    
621        if (rc->param.use_alt_curve <= 0) rc->param.use_alt_curve = 0;
622        if (rc->param.alt_curve_high_dist <= 0) rc->param.alt_curve_high_dist = 500;
623        if (rc->param.alt_curve_low_dist <= 0) rc->param.alt_curve_low_dist = 90;
624        if (rc->param.alt_curve_use_auto <= 0) rc->param.alt_curve_use_auto = 1;
625        if (rc->param.alt_curve_auto_str <= 0) rc->param.alt_curve_auto_str = 30;
626        if (rc->param.alt_curve_type <= 0) rc->param.alt_curve_type = XVID_CURVE_LINEAR;
627        if (rc->param.alt_curve_min_rel_qual <= 0) rc->param.alt_curve_min_rel_qual = 50;
628        if (rc->param.alt_curve_use_auto_bonus_bias <= 0) rc->param.alt_curve_use_auto_bonus_bias = 1;
629        if (rc->param.alt_curve_bonus_bias <= 0) rc->param.alt_curve_bonus_bias = 50;
630    
631        if (rc->param.kftreshold <= 0) rc->param.kftreshold = 10;
632        if (rc->param.kfreduction <= 0) rc->param.kfreduction = 20;
633        if (rc->param.min_key_interval <= 0) rc->param.min_key_interval = 300;
634    
635        if (!det_stats_length(rc, param->filename)){
636            DPRINTF(DPRINTF_RC,"fopen %s failed\n", param->filename);
637            free(rc);
638            return XVID_ERR_FAIL;
639        }
640    
641        if ((rc->stats = malloc(rc->num_frames * sizeof(stat_t))) == NULL) {
642            free(rc);
643            return XVID_ERR_MEMORY;
644        }
645    
646        /* XXX: do we need an addition location */
647        if ((rc->keyframe_locations = malloc((rc->num_keyframes + 1) * sizeof(int))) == NULL) {
648            free(rc->stats);
649            free(rc);
650            return XVID_ERR_MEMORY;
651        }
652    
653        if (!load_stats(rc, param->filename)) {
654            DPRINTF(DPRINTF_RC,"fopen %s failed\n", param->filename);
655            free(rc->keyframe_locations);
656            free(rc->stats);
657            free(rc);
658            return XVID_ERR_FAIL;
659        }
660    
661        /* pre-process our stats */
662    
663            if (rc->num_frames  < create->fbase/create->fincr) {
664                    rc->target = rc->param.bitrate / 8;     /* one second */
665            }else{
666                    rc->target = (rc->param.bitrate * rc->num_frames * create->fincr) / (create->fbase * 8);
667            }
668    
669            rc->target -= rc->num_frames*24;        /* avi file header */
670    
671    
672            pre_process0(rc);
673            if (rc->param.bitrate) {
674            zone_process(rc, create);
675                    internal_scale(rc);
676        }else{
677            /* external scaler: ignore zone */
678            for (i=0;i<rc->num_frames;i++) {
679                rc->stats[i].zone_mode = XVID_ZONE_WEIGHT;
680                rc->stats[i].weight = 1.0;
681            }
682            rc->avg_weight = 1.0;
683            rc->tot_quant = 0;
684        }
685            pre_process1(rc);
686    
687        for (i=0; i<32;i++) {
688            rc->pquant_error[i] = 0;
689            rc->bquant_error[i] = 0;
690            rc->quant_count[i] = 0;
691        }
692    
693        rc->fq_error = 0;
694    
695        *handle = rc;
696            return(0);
697    }
698    
699    
700    static int rc_2pass2_destroy(rc_2pass2_t * rc, xvid_plg_destroy_t * destroy)
701    {
702        free(rc->keyframe_locations);
703        free(rc->stats);
704            free(rc);
705            return(0);
706    }
707    
708    
709    
710    static int rc_2pass2_before(rc_2pass2_t * rc, xvid_plg_data_t * data)
711    {
712        stat_t * s = &rc->stats[data->frame_num];
713        int overflow;
714        int desired;
715        double dbytes;
716        double curve_temp;
717        int capped_to_max_framesize = 0;
718    
719        if (data->quant <= 0) {
720    
721            if (s->zone_mode == XVID_ZONE_QUANT) {
722    
723                rc->fq_error += s->weight;
724                data->quant = (int)rc->fq_error;
725                rc->fq_error -= data->quant;
726    
727            }else { /* XVID_ZONE_WEIGHT */
728    
729                if (data->frame_num >= rc->num_frames) {
730                    /* insufficent stats data */
731                    return 0;
732                }
733    
734                overflow = rc->overflow / 8;        /* XXX: why by 8 */
735    
736                if (s->type == XVID_TYPE_IVOP) {        /* XXX: why */
737                    overflow = 0;
738                }
739    
740                desired = s->scaled_length;
741    
742                dbytes = desired;
743                if (s->type == XVID_TYPE_IVOP) {
744                    dbytes += desired * rc->param.keyframe_boost / 100;
745                }
746                dbytes /= rc->movie_curve;
747    
748                if (s->type == XVID_TYPE_BVOP) {
749                    dbytes *= rc->avg_length[XVID_TYPE_PVOP-1] / rc->avg_length[XVID_TYPE_BVOP-1];
750                }
751    
752                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    
760                            if (labs(desired) > fabs(rc->curve_comp_error)) {
761                                    desired = (int)rc->curve_comp_error;
762                            }
763                }
764    
765                rc->curve_comp_error -= desired;
766    
767                /* alt curve */
768    
769                curve_temp = 0; /* XXX: warning */
770    
771                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    
807                                    curve_temp = curve_temp * rc->curve_comp_scale + rc->alt_curve_curve_bias_bonus;
808    
809                                    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    
815                                    desired += ((int)dbytes);
816                                    rc->curve_comp_error += dbytes - (int)dbytes;
817                            }
818    
819                }else if ((rc->param.curve_compression_high + rc->param.curve_compression_low) &&   s->type != XVID_TYPE_IVOP) {
820    
821                    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                }
842    
843                    if (desired > s->length){
844                            rc->curve_comp_error += desired - s->length;
845                            desired = s->length;
846                    }else{
847                    if (desired < rc->min_length[s->type-1]) {
848                        if (s->type == XVID_TYPE_IVOP){
849                            rc->curve_comp_error -= rc->min_length[XVID_TYPE_IVOP-1] - desired;
850                        }
851                        desired = rc->min_length[s->type-1];
852                    }
853                    }
854    
855                s->desired_length = desired;
856    
857    
858                /* if this keyframe is too close to the next, reduce it's byte allotment
859                XXX: why do we do this after setting the desired length  */
860    
861                    if (s->type == XVID_TYPE_IVOP) {
862                            int KFdistance = rc->keyframe_locations[rc->KF_idx] - rc->keyframe_locations[rc->KF_idx - 1];
863    
864                    if (KFdistance < rc->param.kftreshold) {
865    
866                        KFdistance = KFdistance - rc->param.min_key_interval;
867    
868                                    if (KFdistance >= 0) {
869                            int KF_min_size;
870    
871                                            KF_min_size = desired * (100 - rc->param.kfreduction) / 100;
872                                            if (KF_min_size < 1)
873                                                    KF_min_size = 1;
874    
875                                            desired = KF_min_size + (desired - KF_min_size) * KFdistance /
876                                                    (rc->param.kftreshold - rc->param.min_key_interval);
877    
878                                            if (desired < 1)
879                                                    desired = 1;
880                                    }
881                            }
882                    }
883    
884                overflow = (int)((double)overflow * desired / rc->avg_length[XVID_TYPE_PVOP-1]);
885    
886                    // Foxer: reign in overflow with huge frames
887                    if (labs(overflow) > labs(rc->overflow)) {
888                            overflow = rc->overflow;
889                    }
890    
891                // Foxer: make sure overflow doesn't run away
892    
893                    if (overflow > desired * rc->param.max_overflow_improvement / 100) {
894                            desired += (overflow <= desired) ? desired * rc->param.max_overflow_improvement / 100 :
895                                    overflow * rc->param.max_overflow_improvement / 100;
896                    }else if (overflow < desired * rc->param.max_overflow_degradation / -100){
897                            desired += desired * rc->param.max_overflow_degradation / -100;
898                    }else{
899                            desired += overflow;
900                    }
901    
902                if (desired > rc->max_length) {
903                            capped_to_max_framesize = 1;
904                            desired = rc->max_length;
905                    }
906    
907                // make sure to not scale below the minimum framesize
908                if (desired < rc->min_length[s->type-1]) {
909                    desired = rc->min_length[s->type-1];
910                }
911    
912    
913                // very 'simple' quant<->filesize relationship
914                data->quant= (s->quant * s->length) / desired;
915    
916                    if (data->quant < 1) {
917                            data->quant = 1;
918                } else if (data->quant > 31) {
919                            data->quant = 31;
920                    }
921                    else if (s->type != XVID_TYPE_IVOP)
922                    {
923                            // Foxer: aid desired quantizer precision by accumulating decision error
924                            if (s->type== XVID_TYPE_BVOP) {
925                                    rc->bquant_error[data->quant] += ((double)(s->quant * s->length) / desired) - data->quant;
926    
927                                    if (rc->bquant_error[data->quant] >= 1.0) {
928                                            rc->bquant_error[data->quant] -= 1.0;
929                                            data->quant++;
930                                    }
931                            }else{
932                                    rc->pquant_error[data->quant] += ((double)(s->quant * s->length) / desired) - data->quant;
933    
934                        if (rc->pquant_error[data->quant] >= 1.0) {
935                                            rc->pquant_error[data->quant] -= 1.0;
936                                            ++data->quant;
937                                    }
938                            }
939                    }
940    
941                /* cap to min/max quant */
942    
943                if (data->quant < data->min_quant[s->type-1]) {
944                    data->quant = data->min_quant[s->type-1];
945                }else if (data->quant > data->max_quant[s->type-1]) {
946                    data->quant = data->max_quant[s->type-1];
947                }
948    
949                /* subsequent p/b frame quants can only be +- 2 */
950                    if (s->type != XVID_TYPE_IVOP && rc->last_quant[s->type-1] && capped_to_max_framesize == 0) {
951    
952                            if (data->quant > rc->last_quant[s->type-1] + 2) {
953                                    data->quant = rc->last_quant[s->type-1] + 2;
954                                    DPRINTF(DPRINTF_RC, "p/b-frame quantizer prevented from rising too steeply");
955                            }
956                            if (data->quant < rc->last_quant[s->type-1] - 2) {
957                                    data->quant = rc->last_quant[s->type-1] - 2;
958                                    DPRINTF(DPRINTF_RC, "p/b-frame quantizer prevented from falling too steeply");
959                            }
960                    }
961    
962                    if (capped_to_max_framesize == 0) {
963                    rc->last_quant[s->type-1] = data->quant;
964                    }
965    
966    
967            }   /* if */
968    
969        }
970    
971        return 0;
972    }
973    
974    
975    
976    static int rc_2pass2_after(rc_2pass2_t * rc, xvid_plg_data_t * data)
977    {
978        stat_t * s = &rc->stats[data->frame_num];
979    
980        if (data->frame_num >= rc->num_frames) {
981            /* insufficent stats data */
982            return 0;
983        }
984    
985        rc->quant_count[data->quant]++;
986    
987        if (data->type == XVID_TYPE_IVOP) {
988            int kfdiff = (rc->keyframe_locations[rc->KF_idx] -      rc->keyframe_locations[rc->KF_idx - 1]);
989    
990            rc->overflow += rc->KFoverflow;
991            rc->KFoverflow = s->desired_length - data->length;
992    
993            if (kfdiff > 1) {  // non-consecutive keyframes
994                rc->KFoverflow_partial = rc->KFoverflow / (kfdiff - 1);
995            }else{ // consecutive keyframes
996                            rc->overflow += rc->KFoverflow;
997                            rc->KFoverflow = 0;
998                            rc->KFoverflow_partial = 0;
999            }
1000            rc->KF_idx++;
1001        }else{
1002            // distribute part of the keyframe overflow
1003            rc->overflow += s->desired_length - data->length + rc->KFoverflow_partial;
1004            rc->KFoverflow -= rc->KFoverflow_partial;
1005        }
1006    
1007        printf("[%i] quant:%i stats1:%i scaled:%i actual:%i overflow:%i\n",
1008            data->frame_num,
1009            data->quant,
1010            s->length,
1011            s->scaled_length,
1012            data->length,
1013            rc->overflow);
1014    
1015        return(0);
1016    }
1017    
1018    
1019    
1020    int xvid_plugin_2pass2(void * handle, int opt, void * param1, void * param2)
1021    {
1022        switch(opt)
1023        {
1024        case XVID_PLG_INFO :
1025            return 0;
1026    
1027        case XVID_PLG_CREATE :
1028            return rc_2pass2_create((xvid_plg_create_t*)param1, param2);
1029    
1030        case XVID_PLG_DESTROY :
1031            return rc_2pass2_destroy((rc_2pass2_t*)handle, (xvid_plg_destroy_t*)param1);
1032    
1033        case XVID_PLG_BEFORE :
1034            return rc_2pass2_before((rc_2pass2_t*)handle, (xvid_plg_data_t*)param1);
1035    
1036        case XVID_PLG_AFTER :
1037            return rc_2pass2_after((rc_2pass2_t*)handle, (xvid_plg_data_t*)param1);
1038        }
1039    
1040        return XVID_ERR_FAIL;
1041    }

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.1.2.4

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