[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.38, Sun May 9 14:00:35 2004 UTC
# Line 0  Line 1 
1    /******************************************************************************
2     *
3     *  XviD Bit Rate Controller Library
4     *  - VBR 2 pass bitrate controller implementation -
5     *
6     *  Copyright (C)      2002 Foxer <email?>
7     *                     2002 Dirk Knop <dknop@gwdg.de>
8     *                2002-2003 Edouard Gomez <ed.gomez@free.fr>
9     *                     2003 Pete Ross <pross@xvid.org>
10     *
11     *  This curve treatment algorithm is the one originally implemented by Foxer
12     *  and tuned by Dirk Knop for the XviD vfw frontend.
13     *
14     *  This program is free software; you can redistribute it and/or modify
15     *  it under the terms of the GNU General Public License as published by
16     *  the Free Software Foundation; either version 2 of the License, or
17     *  (at your option) any later version.
18     *
19     *  This program is distributed in the hope that it will be useful,
20     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22     *  GNU General Public License for more details.
23     *
24     *  You should have received a copy of the GNU General Public License
25     *  along with this program; if not, write to the Free Software
26     *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27     *
28     * $Id$
29     *
30     *****************************************************************************/
31    
32    #define BQUANT_PRESCALE
33    #undef COMPENSATE_FORMULA
34    
35    /* forces second pass not to be bigger than first */
36    #undef PASS_SMALLER
37    
38    /* automtically alters overflow controls (strength and improvement/degradation)
39            to fight most common problems without user's knowladge */
40    #define SMART_OVERFLOW_SETTING
41    
42    #include <stdio.h>
43    #include <math.h>
44    #include <limits.h>
45    
46    #include "../xvid.h"
47    #include "../image/image.h"
48    
49    /*****************************************************************************
50     * Some default settings
51     ****************************************************************************/
52    
53    #define DEFAULT_KEYFRAME_BOOST 0
54    #define DEFAULT_OVERFLOW_CONTROL_STRENGTH 10
55    #define DEFAULT_CURVE_COMPRESSION_HIGH 0
56    #define DEFAULT_CURVE_COMPRESSION_LOW 0
57    #define DEFAULT_MAX_OVERFLOW_IMPROVEMENT 10
58    #define DEFAULT_MAX_OVERFLOW_DEGRADATION 10
59    
60    /* Keyframe settings */
61    #define DEFAULT_KFREDUCTION 20
62    #define DEFAULT_KFTHRESHOLD 1
63    
64    /*****************************************************************************
65     * Some default constants (can be tuned)
66     ****************************************************************************/
67    
68    /* Specify the invariant part of the headers bits (header+MV)
69     * as  hlength/cst */
70    #define INVARIANT_HEADER_PART_IVOP 1 /* factor 1.0f   */
71    #define INVARIANT_HEADER_PART_PVOP 2 /* factor 0.5f   */
72    #define INVARIANT_HEADER_PART_BVOP 8 /* factor 0.125f */
73    
74    /*****************************************************************************
75     * Structures
76     ****************************************************************************/
77    
78    /* Statistics */
79    typedef struct {
80            int type;               /* first pass type */
81            int quant;              /* first pass quant */
82            int blks[3];            /* k,m,y blks */
83            int length;             /* first pass length */
84            int invariant;          /* what we assume as being invariant between the two passes, it's a sub part of header + MV bits */
85            int scaled_length;      /* scaled length */
86            int desired_length;     /* desired length; calculated during encoding */
87            int error;
88    
89            int zone_mode;   /* XVID_ZONE_xxx */
90            double weight;
91    } twopass_stat_t;
92    
93    /* Context struct */
94    typedef struct
95    {
96            xvid_plugin_2pass2_t param;
97    
98            /*----------------------------------
99             * constant statistical data
100             *--------------------------------*/
101    
102            /* Number of frames of the sequence */
103            int num_frames;
104    
105            /* Number of Intra frames of the sequence */
106            int num_keyframes;
107    
108            /* Target filesize to reach */
109            uint64_t target;
110    
111            /* Count of each frame types */
112            int count[3];
113    
114            /* Total length of each frame types (1st pass) */
115            uint64_t tot_length[3];
116            uint64_t tot_invariant[3];
117    
118            /* Average length of each frame types (used first for 1st pass data and
119             * then for scaled averages */
120            double avg_length[3];
121    
122            /* Minimum frame length allowed for each frame type */
123            int min_length[3];
124    
125            /* Total bytes per frame type once the curve has been scaled
126             * NB: advanced parameters do not change this value. This field
127             *     represents the total scaled w/o any advanced settings */
128            uint64_t tot_scaled_length[3];
129    
130            /* Maximum observed frame size observed during the first pass, the RC
131             * will try tp force all frame sizes in the second pass to be under that
132             * limit */
133            int max_length;
134    
135            /*----------------------------------
136             * Zones statistical data
137             *--------------------------------*/
138    
139            /* Total length used by XVID_ZONE_QUANT zones */
140            uint64_t tot_quant;
141            uint64_t tot_quant_invariant;
142    
143            /* Holds the total amount of frame bytes, zone weighted (only scalable
144             * part of frame bytes) */
145            uint64_t tot_weighted;
146    
147            /*----------------------------------
148             * Advanced settings helper ratios
149             *--------------------------------*/
150    
151            /* This the ratio that has to be applied to all p/b frames in order
152             * to reserve/retrieve bits for/from keyframe boosting and consecutive
153             * keyframe penalty */
154            double pb_iboost_tax_ratio;
155    
156            /* This the ratio to apply to all b/p frames in order to respect the
157             * assymetric curve compression while respecting a target filesize
158             * NB: The assymetric delta gain has to be computed before this ratio
159             *     is applied, and then the delta is added to the scaled size */
160            double assymetric_tax_ratio;
161    
162            /*----------------------------------
163             * Data from the stats file kept
164             * into RAM for easy access
165             *--------------------------------*/
166    
167            /* Array of keyframe locations
168             * eg: rc->keyframe_locations[100] returns the frame number of the 100th
169             *     keyframe */
170            int *keyframe_locations;
171    
172            /* Index of the last keyframe used in the keyframe_location */
173            int KF_idx;
174    
175            /* Array of all 1st pass data file -- see the twopass_stat_t structure
176             * definition for more details */
177            twopass_stat_t * stats;
178    
179            /*----------------------------------
180             * Hysteresis helpers
181             *--------------------------------*/
182    
183            /* This field holds the int2float conversion errors of each quant per
184             * frame type, this allow the RC to keep track of rouding error and thus
185             * increase or decrease the chosen quant according to this residue */
186            double quant_error[3][32];
187    
188            /* This fields stores the count of each quant usage per frame type
189             * No real role but for debugging */
190            int quant_count[3][32];
191    
192            /* Last valid quantizer used per frame type, it allows quantizer
193             * increament/decreament limitation in order to avoid big image quality
194             * "jumps" */
195            int last_quant[3];
196    
197            /*----------------------------------
198             * Overflow control
199             *--------------------------------*/
200    
201            /* Current overflow that has to be distributed to p/b frames */
202            double overflow;
203    
204            /* Total overflow for keyframes -- not distributed directly */
205            double KFoverflow;
206    
207            /* Amount of keyframe overflow to introduce to the global p/b frame
208             * overflow counter at each encoded frame */
209            double KFoverflow_partial;
210    
211            /* Unknown ???
212             * ToDo: description */
213            double fq_error;
214    
215            int min_quant; /* internal minimal quant, prevents wrong quants from being used */
216    
217            /*----------------------------------
218             * Debug
219             *--------------------------------*/
220            double desired_total;
221            double real_total;
222    } rc_2pass2_t;
223    
224    
225    /*****************************************************************************
226     * Sub plugin functions prototypes
227     ****************************************************************************/
228    
229    static int rc_2pass2_create(xvid_plg_create_t * create, rc_2pass2_t ** handle);
230    static int rc_2pass2_before(rc_2pass2_t * rc, xvid_plg_data_t * data);
231    static int rc_2pass2_after(rc_2pass2_t * rc, xvid_plg_data_t * data);
232    static int rc_2pass2_destroy(rc_2pass2_t * rc, xvid_plg_destroy_t * destroy);
233    
234    /*****************************************************************************
235     * Plugin definition
236     ****************************************************************************/
237    
238    int
239    xvid_plugin_2pass2(void * handle, int opt, void * param1, void * param2)
240    {
241            switch(opt) {
242            case XVID_PLG_INFO :
243            case XVID_PLG_FRAME :
244                    return 0;
245    
246            case XVID_PLG_CREATE :
247                    return rc_2pass2_create((xvid_plg_create_t*)param1, param2);
248    
249            case XVID_PLG_DESTROY :
250                    return rc_2pass2_destroy((rc_2pass2_t*)handle, (xvid_plg_destroy_t*)param1);
251    
252            case XVID_PLG_BEFORE :
253                    return rc_2pass2_before((rc_2pass2_t*)handle, (xvid_plg_data_t*)param1);
254    
255            case XVID_PLG_AFTER :
256                    return rc_2pass2_after((rc_2pass2_t*)handle, (xvid_plg_data_t*)param1);
257            }
258    
259            return XVID_ERR_FAIL;
260    }
261    
262    /*****************************************************************************
263     * Sub plugin functions definitions
264     ****************************************************************************/
265    
266    /* First a few local helping function prototypes */
267    static  int statsfile_count_frames(rc_2pass2_t * rc, char * filename);
268    static  int statsfile_load(rc_2pass2_t *rc, char * filename);
269    static void zone_process(rc_2pass2_t *rc, const xvid_plg_create_t * create);
270    static void first_pass_stats_prepare_data(rc_2pass2_t * rc);
271    static void first_pass_scale_curve_internal(rc_2pass2_t *rc);
272    static void scaled_curve_apply_advanced_parameters(rc_2pass2_t * rc);
273    #ifdef VBV
274    static  int check_curve_for_vbv_compliancy(rc_2pass2_t * rc, const float fps);
275    static  int scale_curve_for_vbv_compliancy(rc_2pass2_t * rc, const float fps);
276    #endif
277    
278    #if 0
279    static void stats_print(rc_2pass2_t * rc);
280    #endif
281    
282    /*----------------------------------------------------------------------------
283     *--------------------------------------------------------------------------*/
284    
285    static int
286    rc_2pass2_create(xvid_plg_create_t * create, rc_2pass2_t **handle)
287    {
288            xvid_plugin_2pass2_t * param = (xvid_plugin_2pass2_t *)create->param;
289            rc_2pass2_t * rc;
290            int i;
291    
292            rc = malloc(sizeof(rc_2pass2_t));
293            if (rc == NULL)
294                    return XVID_ERR_MEMORY;
295    
296            rc->param = *param;
297    
298            /* Initialize all defaults */
299    #define _INIT(a, b) if((a) <= 0) (a) = (b)
300            /* Let's set our defaults if needed */
301            _INIT(rc->param.keyframe_boost, DEFAULT_KEYFRAME_BOOST);
302            _INIT(rc->param.overflow_control_strength, DEFAULT_OVERFLOW_CONTROL_STRENGTH);
303            _INIT(rc->param.curve_compression_high, DEFAULT_CURVE_COMPRESSION_HIGH);
304            _INIT(rc->param.curve_compression_low, DEFAULT_CURVE_COMPRESSION_LOW);
305            _INIT(rc->param.max_overflow_improvement, DEFAULT_MAX_OVERFLOW_IMPROVEMENT);
306            _INIT(rc->param.max_overflow_degradation,  DEFAULT_MAX_OVERFLOW_DEGRADATION);
307    
308            /* Keyframe settings */
309            _INIT(rc->param.kfreduction, DEFAULT_KFREDUCTION);
310            _INIT(rc->param.kfthreshold, DEFAULT_KFTHRESHOLD);
311    #undef _INIT
312    
313            /* Initialize some stuff to zero */
314            for(i=0; i<3; i++) {
315                    int j;
316                    for (j=0; j<32; j++) {
317                            rc->quant_error[i][j] = 0;
318                            rc->quant_count[i][j] = 0;
319                    }
320            }
321    
322            for (i=0; i<3; i++) rc->last_quant[i] = 0;
323    
324            rc->fq_error = 0;
325            rc->min_quant = 1;
326    
327            /* Count frames (and intra frames) in the stats file, store the result into
328             * the rc structure */
329            if (statsfile_count_frames(rc, param->filename) == -1) {
330                    DPRINTF(XVID_DEBUG_RC,"[xvid rc] -- ERROR: fopen %s failed\n", param->filename);
331                    free(rc);
332                    return(XVID_ERR_FAIL);
333            }
334    
335            /* Allocate the stats' memory */
336            if ((rc->stats = malloc(rc->num_frames * sizeof(twopass_stat_t))) == NULL) {
337                    free(rc);
338                    return(XVID_ERR_MEMORY);
339            }
340    
341            /* Allocate keyframes location's memory
342             * PS: see comment in pre_process0 for the +1 location requirement */
343            rc->keyframe_locations = malloc((rc->num_keyframes + 1) * sizeof(int));
344            if (rc->keyframe_locations == NULL) {
345                    free(rc->stats);
346                    free(rc);
347                    return(XVID_ERR_MEMORY);
348            }
349    
350            /* Load the first pass stats */
351            if (statsfile_load(rc, param->filename) == -1) {
352                    DPRINTF(XVID_DEBUG_RC,"[xvid rc] -- ERROR: fopen %s failed\n", param->filename);
353                    free(rc->keyframe_locations);
354                    free(rc->stats);
355                    free(rc);
356                    return XVID_ERR_FAIL;
357            }
358    
359            /* Compute the target filesize */
360            if (rc->param.bitrate<0) {
361                    /* if negative, bitrate equals the target (in kbytes) */
362                    rc->target = ((uint64_t)(-rc->param.bitrate)) * 1024;
363            } else if (rc->num_frames  < create->fbase/create->fincr) {
364                    /* Source sequence is less than 1s long, we do as if it was 1s long */
365                    rc->target = rc->param.bitrate / 8;
366            } else {
367                    /* Target filesize = bitrate/8 * numframes / framerate */
368                    rc->target =
369                            ((uint64_t)rc->param.bitrate * (uint64_t)rc->num_frames * \
370                             (uint64_t)create->fincr) / \
371                            ((uint64_t)create->fbase * 8);
372            }
373    
374            DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- Frame rate: %d/%d (%ffps)\n",
375                            create->fbase, create->fincr,
376                            (double)create->fbase/(double)create->fincr);
377            DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- Number of frames: %d\n", rc->num_frames);
378            if(rc->param.bitrate>=0)
379                    DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- Target bitrate: %ld\n", rc->param.bitrate);
380            DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- Target filesize: %lld\n", rc->target);
381    
382            /* Compensate the average frame overhead caused by the container */
383            rc->target -= rc->num_frames*rc->param.container_frame_overhead;
384            DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- Container Frame overhead: %d\n", rc->param.container_frame_overhead);
385            if(rc->param.container_frame_overhead)
386                    DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- New target filesize after container compensation: %lld\n", rc->target);
387    
388            /* When bitrate is not given it means it has been scaled by an external
389             * application */
390            if (rc->param.bitrate) {
391                    /* Apply zone settings
392                     * - set rc->tot_quant which represents the total num of bytes spent in
393                     *   fixed quant zones
394                     * - set rc->tot_weighted which represents the total amount of bytes
395                     *   spent in normal or weighted zones in first pass (normal zones can
396                     *   be considered weight=1)
397                     * - set rc->tot_quant_invariant which represents the total num of bytes
398                     *   spent in fixed quant zones for headers */
399                    zone_process(rc, create);
400            } else {
401                    /* External scaling -- zones are ignored */
402                    for (i=0;i<rc->num_frames;i++) {
403                            rc->stats[i].zone_mode = XVID_ZONE_WEIGHT;
404                            rc->stats[i].weight = 1.0;
405                    }
406                    rc->tot_quant = 0;
407            }
408    
409            /* Gathers some information about first pass stats:
410             *  - finds the minimum frame length for each frame type during 1st pass.
411             *     rc->min_size[]
412             *  - determines the maximum frame length observed (no frame type distinction).
413             *     rc->max_size
414             *  - count how many times each frame type has been used.
415             *     rc->count[]
416             *  - total bytes used per frame type
417             *     rc->tot_length[]
418             *  - total bytes considered invariant between the 2 passes
419             *  - store keyframe location
420             *     rc->keyframe_locations[]
421             */
422            first_pass_stats_prepare_data(rc);
423    
424            /* If we have a user bitrate, it means it's an internal curve scaling */
425            if (rc->param.bitrate) {
426                    /* Perform internal curve scaling */
427                    first_pass_scale_curve_internal(rc);
428            }
429    
430            /* Apply advanced curve options, and compute some parameters in order to
431             * shape the curve in the BEFORE/AFTER pair of functions */
432            scaled_curve_apply_advanced_parameters(rc);
433    
434    
435    #ifdef VBV
436    /* Check curve for VBV compliancy and rescale if necessary */
437    
438    
439    #ifdef VBV_FORCE
440      if (rc->param.vbvsize==0)
441      {
442        rc->param.vbvsize      =  3145728;
443        rc->param.vbvinitial   =  2359296;
444        rc->param.vbv_maxrate  =  4000000;
445        rc->param.vbv_peakrate = 10000000;
446      }
447    #endif
448    
449      if (rc->param.vbvsize>0)    /* vbvsize==0 switches VBV check off */
450      {
451        const double fps = (double)create->fbase/(double)create->fincr;
452        int status = check_curve_for_vbv_compliancy(rc, fps);
453    #ifdef VBV_DEBUG
454        if (status)
455          fprintf(stderr,"underflow detected\n Scaling Curve for compliancy... ");
456    #endif
457    
458            status = scale_curve_for_vbv_compliancy(rc, fps);
459    
460    #ifdef VBV_DEBUG
461          if (status==0)
462            fprintf(stderr,"done.\n");
463          else
464            fprintf(stderr,"impossible.\n");
465    #endif
466      }
467    #endif
468    
469            *handle = rc;
470            return(0);
471    }
472    
473    /*----------------------------------------------------------------------------
474     *--------------------------------------------------------------------------*/
475    
476    static int
477    rc_2pass2_destroy(rc_2pass2_t * rc, xvid_plg_destroy_t * destroy)
478    {
479            DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- target_total:%lld desired_total:%.2f (%.2f%%) actual_total:%.2f (%.2f%%)\n",
480                            rc->target,
481                            rc->desired_total,
482                            100*rc->desired_total/(double)rc->target,
483                            rc->real_total,
484                            100*rc->real_total/(double)rc->target);
485    
486            free(rc->keyframe_locations);
487            free(rc->stats);
488            free(rc);
489            return(0);
490    }
491    
492    /*----------------------------------------------------------------------------
493     *--------------------------------------------------------------------------*/
494    
495    static int
496    rc_2pass2_before(rc_2pass2_t * rc, xvid_plg_data_t * data)
497    {
498            twopass_stat_t * s = &rc->stats[data->frame_num];
499            double dbytes;
500            double scaled_quant;
501            double overflow;
502            int capped_to_max_framesize = 0;
503    
504            /* This function is quite long but easy to understand. In order to simplify
505             * the code path (a bit), we treat 3 cases that can return immediatly. */
506    
507            /* First case: Another plugin has already set a quantizer */
508            if (data->quant > 0)
509                    return(0);
510    
511            /* Second case: insufficent stats data
512             * We can't guess much what we should do, let core decide all alone */
513            if (data->frame_num >= rc->num_frames) {
514                    DPRINTF(XVID_DEBUG_RC,"[xvid rc] -- stats file too short (now processing frame %d)",
515                            data->frame_num);
516                    return(0);
517            }
518    
519            /* Third case: We are in a Quant zone
520             * Quant zones must just ensure we use the same settings as first pass
521             * So set the quantizer and the type */
522            if (s->zone_mode == XVID_ZONE_QUANT) {
523                    /* Quant stuff */
524                    rc->fq_error += s->weight;
525                    data->quant = (int)rc->fq_error;
526                    rc->fq_error -= data->quant;
527    
528                    /* The type stuff */
529                    data->type = s->type;
530    
531                    /* The only required data for AFTER step is this one for the overflow
532                     * control */
533                    s->desired_length = s->length;
534    
535                    return(0);
536            }
537    
538    
539            /*************************************************************************/
540            /*************************************************************************/
541            /*************************************************************************/
542    
543            /*-------------------------------------------------------------------------
544             * Frame bit allocation first part
545             *
546             * First steps apply user settings, just like it is done in the theoritical
547             * scaled_curve_apply_advanced_parameters
548             *-----------------------------------------------------------------------*/
549    
550            /* Set desired to what we are wanting to obtain for this frame */
551            dbytes = (double)s->scaled_length;
552    
553            /* IFrame user settings*/
554            if (s->type == XVID_TYPE_IVOP) {
555                    /* Keyframe boosting -- All keyframes benefit from it */
556                    dbytes += dbytes*rc->param.keyframe_boost / 100;
557    
558    #if 0 /* ToDo: decide how to apply kfthresholding */
559    #endif
560            } else {
561    
562                    /* P/S/B frames must reserve some bits for iframe boosting */
563                    dbytes *= rc->pb_iboost_tax_ratio;
564    
565                    /* Apply assymetric curve compression */
566                    if (rc->param.curve_compression_high || rc->param.curve_compression_low) {
567                            double assymetric_delta;
568    
569                            /* Compute the assymetric delta, this is computed before applying
570                             * the tax, as done in the pre_process function */
571                            if (dbytes > rc->avg_length[s->type-1])
572                                    assymetric_delta = (rc->avg_length[s->type-1] - dbytes) * rc->param.curve_compression_high / 100.0;
573                            else
574                                    assymetric_delta = (rc->avg_length[s->type-1] - dbytes) * rc->param.curve_compression_low  / 100.0;
575    
576                            /* Now we must apply the assymetric tax, else our curve compression
577                             * would not give a theoritical target size equal to what it is
578                             * expected */
579                            dbytes *= rc->assymetric_tax_ratio;
580    
581                            /* Now we can add the assymetric delta */
582                            dbytes += assymetric_delta;
583                    }
584            }
585    
586            /* That is what we would like to have -- Don't put that chunk after
587             * overflow control, otherwise, overflow is counted twice and you obtain
588             * half sized bitrate sequences */
589            s->desired_length  = (int)dbytes;
590            rc->desired_total += dbytes;
591    
592            /*------------------------------------------------------------------------
593             * Frame bit allocation: overflow control part.
594             *
595             * Unlike the theoritical scaled_curve_apply_advanced_parameters, here
596             * it's real encoding and we need to make sure we don't go so far from
597             * what is our ideal scaled curve.
598             *-----------------------------------------------------------------------*/
599    
600            /* Compute the overflow we should compensate */
601            if (s->type != XVID_TYPE_IVOP || rc->overflow > 0) {
602                    double frametype_factor;
603                    double framesize_factor;
604    
605                    /* Take only the desired part of overflow */
606                    overflow = rc->overflow;
607    
608                    /* Factor that will take care to decrease the overflow applied
609                     * according to the importance of this frame type in term of
610                     * overall size */
611                    frametype_factor  = rc->count[XVID_TYPE_IVOP-1]*rc->avg_length[XVID_TYPE_IVOP-1];
612                    frametype_factor += rc->count[XVID_TYPE_PVOP-1]*rc->avg_length[XVID_TYPE_PVOP-1];
613                    frametype_factor += rc->count[XVID_TYPE_BVOP-1]*rc->avg_length[XVID_TYPE_BVOP-1];
614                    frametype_factor /= rc->count[s->type-1]*rc->avg_length[s->type-1];
615                    frametype_factor  = 1/frametype_factor;
616    
617                    /* Factor that will take care not to compensate too much for this frame
618                     * size */
619                    framesize_factor  = dbytes;
620                    framesize_factor /= rc->avg_length[s->type-1];
621    
622                    /* Treat only the overflow part concerned by this frame type and size */
623                    overflow *= frametype_factor;
624    #if 0
625                    /* Leave this one alone, as it impacts badly on quality */
626                    overflow *= framesize_factor;
627    #endif
628    
629                    /* Apply the overflow strength imposed by the user */
630                    overflow *= (rc->param.overflow_control_strength/100.0f);
631            } else {
632                    /* no negative overflow applied in IFrames because:
633                     *  - their role is important as they're references for P/BFrames.
634                     *  - there aren't much in typical sequences, so if an IFrame overflows too
635                     *    much, this overflow may impact the next IFrame too much and generate
636                     *    a sequence of poor quality frames */
637                    overflow = 0;
638            }
639    
640            /* Make sure we are not trying to compensate more overflow than we even have */
641            if (fabs(overflow) > fabs(rc->overflow))
642                    overflow = rc->overflow;
643    
644            /* Make sure the overflow doesn't make the frame size to get out of the range
645             * [-max_degradation..+max_improvment] */
646            if (overflow > dbytes*rc->param.max_overflow_improvement / 100) {
647                    if(overflow <= dbytes)
648                            dbytes += dbytes * rc->param.max_overflow_improvement / 100;
649                    else
650                            dbytes += overflow * rc->param.max_overflow_improvement / 100;
651            } else if (overflow < - dbytes * rc->param.max_overflow_degradation / 100) {
652                    dbytes -= dbytes * rc->param.max_overflow_degradation / 100;
653            } else {
654                    dbytes += overflow;
655            }
656    
657            /*-------------------------------------------------------------------------
658             * Frame bit allocation last part:
659             *
660             * Cap frame length so we don't reach neither bigger frame sizes than first
661             * pass nor smaller than the allowed minimum.
662             *-----------------------------------------------------------------------*/
663    
664    #ifdef PASS_SMALLER
665            if (dbytes > s->length) {
666                    dbytes = s->length;
667            }
668    #endif
669    
670            /* Prevent stupid desired sizes under logical values */
671            if (dbytes < rc->min_length[s->type-1]) {
672                    dbytes = rc->min_length[s->type-1];
673            }
674    
675            /*------------------------------------------------------------------------
676             * Desired frame length <-> quantizer mapping
677             *-----------------------------------------------------------------------*/
678    
679    #ifdef BQUANT_PRESCALE
680            /* For bframes we prescale the quantizer to avoid too high quant scaling */
681            if(s->type == XVID_TYPE_BVOP) {
682    
683                    twopass_stat_t *b_ref = s;
684    
685                    /* Find the reference frame */
686                    while(b_ref != &rc->stats[0] && b_ref->type == XVID_TYPE_BVOP)
687                            b_ref--;
688    
689                    /* Compute the original quant */
690                    s->quant  = 2*(100*s->quant - data->bquant_offset);
691                    s->quant += data->bquant_ratio - 1; /* to avoid rounding issues */
692                    s->quant  = s->quant/data->bquant_ratio - b_ref->quant;
693            }
694    #endif
695    
696            /* Don't laugh at this very 'simple' quant<->size relationship, it
697             * proves to be acurate enough for our algorithm */
698            scaled_quant = (double)s->quant*(double)s->length/(double)dbytes;
699    
700    #ifdef COMPENSATE_FORMULA
701            /* We know xvidcore will apply the bframe formula again, so we compensate
702             * it right now to make sure we would not apply it twice */
703            if(s->type == XVID_TYPE_BVOP) {
704    
705                    twopass_stat_t *b_ref = s;
706    
707                    /* Find the reference frame */
708                    while(b_ref != &rc->stats[0] && b_ref->type == XVID_TYPE_BVOP)
709                            b_ref--;
710    
711                    /* Compute the quant it would be if the core did not apply the bframe
712                     * formula */
713                    scaled_quant  = 100*scaled_quant - data->bquant_offset;
714                    scaled_quant += data->bquant_ratio - 1; /* to avoid rouding issues */
715                    scaled_quant /= data->bquant_ratio;
716            }
717    #endif
718    
719            /* Quantizer has been scaled using floating point operations/results, we
720             * must cast it to integer */
721            data->quant = (int)scaled_quant;
722    
723            /* Let's clip the computed quantizer, if needed */
724            if (data->quant < 1) {
725                    data->quant = 1;
726            } else if (data->quant > 31) {
727                    data->quant = 31;
728            } else {
729    
730                    /* The frame quantizer has not been clipped, this appears to be a good
731                     * computed quantizer, do not loose quantizer decimal part that we
732                     * accumulate for later reuse when its sum represents a complete
733                     * unit. */
734                    rc->quant_error[s->type-1][data->quant] += scaled_quant - (double)data->quant;
735    
736                    if (rc->quant_error[s->type-1][data->quant] >= 1.0) {
737                            rc->quant_error[s->type-1][data->quant] -= 1.0;
738                            data->quant++;
739                    } else if (rc->quant_error[s->type-1][data->quant] <= -1.0) {
740                            rc->quant_error[s->type-1][data->quant] += 1.0;
741                            data->quant--;
742                    }
743            }
744    
745            /* Now we have a computed quant that is in the right quante range, with a
746             * possible +1 correction due to cumulated error. We can now safely clip
747             * the quantizer again with user's quant ranges. "Safely" means the Rate
748             * Control could learn more about this quantizer, this knowledge is useful
749             * for future frames even if it this quantizer won't be really used atm,
750             * that's why we don't perform this clipping earlier. */
751            if (data->quant < data->min_quant[s->type-1]) {
752                    data->quant = data->min_quant[s->type-1];
753            } else if (data->quant > data->max_quant[s->type-1]) {
754                    data->quant = data->max_quant[s->type-1];
755            }
756    
757            if (data->quant < rc->min_quant) data->quant = rc->min_quant;
758    
759            /* To avoid big quality jumps from frame to frame, we apply a "security"
760             * rule that makes |last_quant - new_quant| <= 2. This rule only applies
761             * to predicted frames (P and B) */
762            if (s->type != XVID_TYPE_IVOP && rc->last_quant[s->type-1] && capped_to_max_framesize == 0) {
763    
764                    if (data->quant > rc->last_quant[s->type-1] + 2) {
765                            data->quant = rc->last_quant[s->type-1] + 2;
766                            DPRINTF(XVID_DEBUG_RC,
767                                            "[xvid rc] -- frame %d p/b-frame quantizer prevented from rising too steeply\n",
768                                            data->frame_num);
769                    }
770                    if (data->quant < rc->last_quant[s->type-1] - 2) {
771                            data->quant = rc->last_quant[s->type-1] - 2;
772                            DPRINTF(XVID_DEBUG_RC,
773                                            "[xvid rc] -- frame:%d p/b-frame quantizer prevented from falling too steeply\n",
774                                            data->frame_num);
775                    }
776            }
777    
778            /* We don't want to pollute the RC histerisis when our computed quant has
779             * been computed from a capped frame size */
780            if (capped_to_max_framesize == 0)
781                    rc->last_quant[s->type-1] = data->quant;
782    
783            /* Don't forget to force 1st pass frame type ;-) */
784            data->type = s->type;
785    
786            return 0;
787    }
788    
789    /*----------------------------------------------------------------------------
790     *--------------------------------------------------------------------------*/
791    
792    static int
793    rc_2pass2_after(rc_2pass2_t * rc, xvid_plg_data_t * data)
794    {
795            const char frame_type[4] = { 'i', 'p', 'b', 's'};
796            twopass_stat_t * s = &rc->stats[data->frame_num];
797    
798            /* Insufficent stats data */
799            if (data->frame_num >= rc->num_frames)
800                    return 0;
801    
802            /* Update the quantizer counter */
803            rc->quant_count[s->type-1][data->quant]++;
804    
805            /* Update the frame type overflow */
806            if (data->type == XVID_TYPE_IVOP) {
807                    int kfdiff = 0;
808    
809                    if(rc->KF_idx != rc->num_frames -1) {
810                            kfdiff  = rc->keyframe_locations[rc->KF_idx+1];
811                            kfdiff -= rc->keyframe_locations[rc->KF_idx];
812                    }
813    
814                    /* Flush Keyframe overflow accumulator */
815                    rc->overflow += rc->KFoverflow;
816    
817                    /* Store the frame overflow to the keyframe accumulator */
818                    rc->KFoverflow = s->desired_length - data->length;
819    
820                    if (kfdiff > 1) {
821                            /* Non-consecutive keyframes case:
822                             * We can then divide this total keyframe overflow into equal parts
823                             * that we will distribute into regular overflow at each frame
824                             * between the sequence bounded by two IFrames */
825                            rc->KFoverflow_partial = rc->KFoverflow / (kfdiff - 1);
826                    } else {
827                            /* Consecutive keyframes case:
828                             * Flush immediatly the keyframe overflow and reset keyframe
829                             * overflow */
830                            rc->overflow += rc->KFoverflow;
831                            rc->KFoverflow = 0;
832                            rc->KFoverflow_partial = 0;
833                    }
834                    rc->KF_idx++;
835            } else {
836                    /* Accumulate the frame overflow */
837                    rc->overflow += s->desired_length - data->length;
838    
839                    /* Distribute part of the keyframe overflow */
840                    rc->overflow += rc->KFoverflow_partial;
841    
842                    /* Don't forget to substract that same amount from the total keyframe
843                     * overflow */
844                    rc->KFoverflow -= rc->KFoverflow_partial;
845            }
846    
847            rc->overflow += (s->error = s->desired_length - data->length);
848            rc->real_total += data->length;
849    
850            DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- frame:%d type:%c quant:%d stats:%d scaled:%d desired:%d actual:%d error:%d overflow:%.2f\n",
851                            data->frame_num,
852                            frame_type[data->type-1],
853                            data->quant,
854                            s->length,
855                            s->scaled_length,
856                            s->desired_length,
857                            s->desired_length - s->error,
858                            -s->error,
859                            rc->overflow);
860    
861            return(0);
862    }
863    
864    /*****************************************************************************
865     * Helper functions definition
866     ****************************************************************************/
867    
868    /* Default buffer size for reading lines */
869    #define BUF_SZ   1024
870    
871    /* Helper functions for reading/parsing the stats file */
872    static char *skipspaces(char *string);
873    static int iscomment(char *string);
874    static char *readline(FILE *f);
875    
876    /* This function counts the number of frame entries in the stats file
877     * It also counts the number of I Frames */
878    static int
879    statsfile_count_frames(rc_2pass2_t * rc, char * filename)
880    {
881            FILE * f;
882            char *line;
883            int lines;
884    
885            rc->num_frames = 0;
886            rc->num_keyframes = 0;
887    
888            if ((f = fopen(filename, "rb")) == NULL)
889                    return(-1);
890    
891            lines = 0;
892            while ((line = readline(f)) != NULL) {
893    
894                    char *ptr;
895                    char type;
896                    int fields;
897    
898                    lines++;
899    
900                    /* We skip spaces */
901                    ptr = skipspaces(line);
902    
903                    /* Skip coment lines or empty lines */
904                    if(iscomment(ptr) || *ptr == '\0') {
905                            free(line);
906                            continue;
907                    }
908    
909                    /* Read the stat line from buffer */
910                    fields = sscanf(ptr, "%c", &type);
911    
912                    /* Valid stats files have at least 7 fields */
913                    if (fields == 1) {
914                            switch(type) {
915                            case 'i':
916                            case 'I':
917                                    rc->num_keyframes++;
918                            case 'p':
919                            case 'P':
920                            case 'b':
921                            case 'B':
922                            case 's':
923                            case 'S':
924                                    rc->num_frames++;
925                                    break;
926                            default:
927                                    DPRINTF(XVID_DEBUG_RC,
928                                                    "[xvid rc] -- WARNING: L%d unknown frame type used (%c).\n",
929                                                    lines, type);
930                            }
931                    } else {
932                                    DPRINTF(XVID_DEBUG_RC,
933                                                    "[xvid rc] -- WARNING: L%d misses some stat fields (%d).\n",
934                                                    lines, 7-fields);
935                    }
936    
937                    /* Free the line buffer */
938                    free(line);
939            }
940    
941            /* We are done with the file */
942            fclose(f);
943    
944            return(0);
945    }
946    
947    /* open stats file(s) and read into rc->stats array */
948    static int
949    statsfile_load(rc_2pass2_t *rc, char * filename)
950    {
951            FILE * f;
952            int processed_entries;
953    
954            /* Opens the file */
955            if ((f = fopen(filename, "rb"))==NULL)
956                    return(-1);
957    
958            processed_entries = 0;
959            while(processed_entries < rc->num_frames) {
960                    char type;
961                    int fields;
962                    twopass_stat_t * s = &rc->stats[processed_entries];
963                    char *line, *ptr;
964    
965                    /* Read the line from the file */
966                    if((line = readline(f)) == NULL)
967                            break;
968    
969                    /* We skip spaces */
970                    ptr = skipspaces(line);
971    
972                    /* Skip comment lines or empty lines */
973                    if(iscomment(ptr) || *ptr == '\0') {
974                            free(line);
975                            continue;
976                    }
977    
978                    /* Reset this field that is optional */
979                    s->scaled_length = 0;
980    
981                    /* Convert the fields */
982                    fields = sscanf(ptr,
983                                                    "%c %d %d %d %d %d %d %d\n",
984                                                    &type,
985                                                    &s->quant,
986                                                    &s->blks[0], &s->blks[1], &s->blks[2],
987                                                    &s->length, &s->invariant /* not really yet */,
988                                                    &s->scaled_length);
989    
990                    /* Free line buffer, we don't need it anymore */
991                    free(line);
992    
993                    /* Fail silently, this has probably been warned in
994                     * statsfile_count_frames */
995                    if(fields != 7 && fields != 8)
996                            continue;
997    
998                    /* Convert frame type and compute the invariant length part */
999                    switch(type) {
1000                    case 'i':
1001                    case 'I':
1002                            s->type = XVID_TYPE_IVOP;
1003                            s->invariant /= INVARIANT_HEADER_PART_IVOP;
1004                            break;
1005                    case 'p':
1006                    case 'P':
1007                    case 's':
1008                    case 'S':
1009                            s->type = XVID_TYPE_PVOP;
1010                            s->invariant /= INVARIANT_HEADER_PART_PVOP;
1011                            break;
1012                    case 'b':
1013                    case 'B':
1014                            s->type = XVID_TYPE_BVOP;
1015                            s->invariant /= INVARIANT_HEADER_PART_BVOP;
1016                            break;
1017                    default:
1018                            /* Same as before, fail silently */
1019                            continue;
1020                    }
1021    
1022                    /* Ok it seems it's been processed correctly */
1023                    processed_entries++;
1024            }
1025    
1026            /* Close the file */
1027            fclose(f);
1028    
1029            return(0);
1030    }
1031    
1032    /* pre-process the statistics data
1033     * - for each type, count, tot_length, min_length, max_length
1034     * - set keyframes_locations, tot_prescaled */
1035    static void
1036    first_pass_stats_prepare_data(rc_2pass2_t * rc)
1037    {
1038            int i,j;
1039    
1040            /* *rc fields initialization
1041             * NB: INT_MAX and INT_MIN are used in order to be immediately replaced
1042             *     with real values of the 1pass */
1043            for (i=0; i<3; i++) {
1044                    rc->count[i]=0;
1045                    rc->tot_length[i] = 0;
1046                    rc->tot_invariant[i] = 0;
1047                    rc->min_length[i] = INT_MAX;
1048            }
1049    
1050            rc->max_length = INT_MIN;
1051            rc->tot_weighted = 0;
1052    
1053            /* Loop through all frames and find/compute all the stuff this function
1054             * is supposed to do */
1055            for (i=j=0; i<rc->num_frames; i++) {
1056                    twopass_stat_t * s = &rc->stats[i];
1057    
1058                    rc->count[s->type-1]++;
1059                    rc->tot_length[s->type-1] += s->length;
1060                    rc->tot_invariant[s->type-1] += s->invariant;
1061                    if (s->zone_mode != XVID_ZONE_QUANT)
1062                            rc->tot_weighted += (int)(s->weight*(s->length - s->invariant));
1063    
1064                    if (s->length < rc->min_length[s->type-1]) {
1065                            rc->min_length[s->type-1] = s->length;
1066                    }
1067    
1068                    if (s->length > rc->max_length) {
1069                            rc->max_length = s->length;
1070                    }
1071    
1072                    if (s->type == XVID_TYPE_IVOP) {
1073                            rc->keyframe_locations[j] = i;
1074                            j++;
1075                    }
1076            }
1077    
1078            /* NB:
1079             * The "per sequence" overflow system considers a natural sequence to be
1080             * formed by all frames between two iframes, so if we want to make sure
1081             * the system does not go nuts during last sequence, we force the last
1082             * frame to appear in the keyframe locations array. */
1083            rc->keyframe_locations[j] = i;
1084    
1085            DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- Min 1st pass IFrame length: %d\n", rc->min_length[0]);
1086            DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- Min 1st pass PFrame length: %d\n", rc->min_length[1]);
1087            DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- Min 1st pass BFrame length: %d\n", rc->min_length[2]);
1088    }
1089    
1090    /* calculate zone weight "center" */
1091    static void
1092    zone_process(rc_2pass2_t *rc, const xvid_plg_create_t * create)
1093    {
1094            int i,j;
1095            int n = 0;
1096    
1097            rc->tot_quant = 0;
1098            rc->tot_quant_invariant = 0;
1099    
1100            if (create->num_zones == 0) {
1101                    for (j = 0; j < rc->num_frames; j++) {
1102                            rc->stats[j].zone_mode = XVID_ZONE_WEIGHT;
1103                            rc->stats[j].weight = 1.0;
1104                    }
1105                    n += rc->num_frames;
1106            }
1107    
1108    
1109            for(i=0; i < create->num_zones; i++) {
1110    
1111                    int next = (i+1<create->num_zones) ? create->zones[i+1].frame : rc->num_frames;
1112    
1113                    /* Zero weight make no sense */
1114                    if (create->zones[i].increment == 0) create->zones[i].increment = 1;
1115                    /* And obviously an undetermined infinite makes even less sense */
1116                    if (create->zones[i].base == 0) create->zones[i].base = 1;
1117    
1118                    if (i==0 && create->zones[i].frame > 0) {
1119                            for (j = 0; j < create->zones[i].frame && j < rc->num_frames; j++) {
1120                                    rc->stats[j].zone_mode = XVID_ZONE_WEIGHT;
1121                                    rc->stats[j].weight = 1.0;
1122                            }
1123                            n += create->zones[i].frame;
1124                    }
1125    
1126                    if (create->zones[i].mode == XVID_ZONE_WEIGHT) {
1127                            for (j = create->zones[i].frame; j < next && j < rc->num_frames; j++ ) {
1128                                    rc->stats[j].zone_mode = XVID_ZONE_WEIGHT;
1129                                    rc->stats[j].weight = (double)create->zones[i].increment / (double)create->zones[i].base;
1130                            }
1131                            next -= create->zones[i].frame;
1132                            n += next;
1133                    } else{  /* XVID_ZONE_QUANT */
1134                            for (j = create->zones[i].frame; j < next && j < rc->num_frames; j++ ) {
1135                                    rc->stats[j].zone_mode = XVID_ZONE_QUANT;
1136                                    rc->stats[j].weight = (double)create->zones[i].increment / (double)create->zones[i].base;
1137                                    rc->tot_quant += rc->stats[j].length;
1138                                    rc->tot_quant_invariant += rc->stats[j].invariant;
1139                            }
1140                    }
1141            }
1142    }
1143    
1144    
1145    /* scale the curve */
1146    static void
1147    first_pass_scale_curve_internal(rc_2pass2_t *rc)
1148    {
1149            int64_t target;
1150            int64_t total_invariant;
1151            double scaler;
1152            int i, num_MBs;
1153    
1154            /* We only scale texture data ! */
1155            total_invariant  = rc->tot_invariant[XVID_TYPE_IVOP-1];
1156            total_invariant += rc->tot_invariant[XVID_TYPE_PVOP-1];
1157            total_invariant += rc->tot_invariant[XVID_TYPE_BVOP-1];
1158            /* don't forget to substract header bytes used in quant zones, otherwise we
1159             * counting them twice */
1160            total_invariant -= rc->tot_quant_invariant;
1161    
1162            /* We remove the bytes used by the fixed quantizer zones during first pass
1163             * with the same quants, so we know very precisely how much that
1164             * represents */
1165            target  = rc->target;
1166            target -= rc->tot_quant;
1167    
1168            /* Let's compute a linear scaler in order to perform curve scaling */
1169            scaler = (double)(target - total_invariant) / (double)(rc->tot_weighted);
1170    
1171    #ifdef SMART_OVERFLOW_SETTING
1172            if (scaler > 0.9) {
1173                    rc->param.max_overflow_degradation *= 5;
1174                    rc->param.max_overflow_improvement *= 5;
1175                    rc->param.overflow_control_strength *= 3;
1176            } else if (scaler > 0.6) {
1177                    rc->param.max_overflow_degradation *= 2;
1178                    rc->param.max_overflow_improvement *= 2;
1179                    rc->param.overflow_control_strength *= 2;
1180            } else {
1181                    rc->min_quant = 2;
1182            }
1183    #endif
1184    
1185            /* Compute min frame lengths (for each frame type) according to the number
1186             * of MBs. We sum all block type counters of frame 0, this gives us the
1187             * number of MBs.
1188             *
1189             * We compare these hardcoded values with observed values in first pass
1190             * (determined in pre_process0).Then we keep the real minimum. */
1191    
1192            /* Number of MBs */
1193            num_MBs  = rc->stats[0].blks[0];
1194            num_MBs += rc->stats[0].blks[1];
1195            num_MBs += rc->stats[0].blks[2];
1196    
1197            /* Minimum for I frames */
1198            if(rc->min_length[XVID_TYPE_IVOP-1] > ((num_MBs*22) + 240) / 8)
1199                    rc->min_length[XVID_TYPE_IVOP-1] = ((num_MBs*22) + 240) / 8;
1200    
1201            /* Minimum for P/S frames */
1202            if(rc->min_length[XVID_TYPE_PVOP-1] > ((num_MBs) + 88)  / 8)
1203                    rc->min_length[XVID_TYPE_PVOP-1] = ((num_MBs) + 88)  / 8;
1204    
1205            /* Minimum for B frames */
1206            if(rc->min_length[XVID_TYPE_BVOP-1] > 8)
1207                    rc->min_length[XVID_TYPE_BVOP-1] = 8;
1208    
1209            /* Perform an initial scale pass.
1210             *
1211             * If a frame size is scaled underneath our hardcoded minimums, then we
1212             * force the frame size to the minimum, and deduct the original & scaled
1213             * frame length from the original and target total lengths */
1214            for (i=0; i<rc->num_frames; i++) {
1215                    twopass_stat_t * s = &rc->stats[i];
1216                    int len;
1217    
1218                    /* No need to scale frame length for which a specific quantizer is
1219                     * specified thanks to zones */
1220                    if (s->zone_mode == XVID_ZONE_QUANT) {
1221                            s->scaled_length = s->length;
1222                            continue;
1223                    }
1224    
1225                    /* Compute the scaled length -- only non invariant data length is scaled */
1226                    len = s->invariant + (int)((double)(s->length-s->invariant) * scaler * s->weight);
1227    
1228                    /* Compare with the computed minimum */
1229                    if (len < rc->min_length[s->type-1]) {
1230                            /* This is a 'forced size' frame, set its frame size to the
1231                             * computed minimum */
1232                            s->scaled_length = rc->min_length[s->type-1];
1233    
1234                            /* Remove both scaled and original size from their respective
1235                             * total counters, as we prepare a second pass for 'regular'
1236                             * frames */
1237                            target -= s->scaled_length;
1238                    } else {
1239                            /* Do nothing for now, we'll scale this later */
1240                            s->scaled_length = 0;
1241                    }
1242            }
1243    
1244            /* The first pass on data substracted all 'forced size' frames from the
1245             * total counters. Now, it's possible to scale the 'regular' frames. */
1246    
1247            /* Scaling factor for 'regular' frames */
1248            scaler = (double)(target - total_invariant) / (double)(rc->tot_weighted);
1249    
1250            /* Do another pass with the new scaler */
1251            for (i=0; i<rc->num_frames; i++) {
1252                    twopass_stat_t * s = &rc->stats[i];
1253    
1254                    /* Ignore frame with forced frame sizes */
1255                    if (s->scaled_length == 0)
1256                            s->scaled_length = s->invariant + (int)((double)(s->length-s->invariant) * scaler * s->weight);
1257            }
1258    
1259            /* Job done */
1260            return;
1261    }
1262    
1263    /* Apply all user settings to the scaled curve
1264     * This implies:
1265     *   keyframe boosting
1266     *   high/low compression */
1267    static void
1268    scaled_curve_apply_advanced_parameters(rc_2pass2_t * rc)
1269    {
1270            int i;
1271            int64_t ivop_boost_total;
1272    
1273            /* Reset the rate controller (per frame type) total byte counters */
1274            for (i=0; i<3; i++) rc->tot_scaled_length[i] = 0;
1275    
1276            /* Compute total bytes for each frame type */
1277            for (i=0; i<rc->num_frames;i++) {
1278                    twopass_stat_t *s = &rc->stats[i];
1279                    rc->tot_scaled_length[s->type-1] += s->scaled_length;
1280            }
1281    
1282            /* First we compute the total amount of bits needed, as being described by
1283             * the scaled distribution. During this pass over the complete stats data,
1284             * we see how much bits two user settings will get/give from/to p&b frames:
1285             *  - keyframe boosting
1286             *  - keyframe distance penalty */
1287            rc->KF_idx = 0;
1288            ivop_boost_total = 0;
1289            for (i=0; i<rc->num_frames; i++) {
1290                    twopass_stat_t * s = &rc->stats[i];
1291    
1292                    /* Some more work is needed for I frames */
1293                    if (s->type == XVID_TYPE_IVOP) {
1294                            int ivop_boost;
1295    
1296                            /* Accumulate bytes needed for keyframe boosting */
1297                            ivop_boost = s->scaled_length*rc->param.keyframe_boost/100;
1298    
1299    #if 0 /* ToDo: decide how to apply kfthresholding */
1300    #endif
1301                            /* If the frame size drops under the minimum length, then cap ivop_boost */
1302                            if (ivop_boost + s->scaled_length < rc->min_length[XVID_TYPE_IVOP-1])
1303                                    ivop_boost = rc->min_length[XVID_TYPE_IVOP-1] - s->scaled_length;
1304    
1305                            /* Accumulate the ivop boost */
1306                            ivop_boost_total += ivop_boost;
1307    
1308                            /* Don't forget to update the keyframe index */
1309                            rc->KF_idx++;
1310                    }
1311            }
1312    
1313            /* Initialize the IBoost tax ratio for P/S/B frames
1314             *
1315             * This ratio has to be applied to p/b/s frames in order to reserve
1316             * additional bits for keyframes (keyframe boosting) or if too much
1317             * keyframe distance is applied, bits retrieved from the keyframes.
1318             *
1319             * ie pb_length *= rc->pb_iboost_tax_ratio;
1320             *
1321             *    gives the ideal length of a p/b frame */
1322    
1323            /* Compute the total length of p/b/s frames (temporary storage into
1324             * movie_curve) */
1325            rc->pb_iboost_tax_ratio  = (double)rc->tot_scaled_length[XVID_TYPE_PVOP-1];
1326            rc->pb_iboost_tax_ratio += (double)rc->tot_scaled_length[XVID_TYPE_BVOP-1];
1327    
1328            /* Compute the ratio described above
1329             *     taxed_total = sum(0, n, tax*scaled_length)
1330             * <=> taxed_total = tax.sum(0, n, scaled_length)
1331             * <=> tax = taxed_total / original_total */
1332            rc->pb_iboost_tax_ratio =
1333                    (rc->pb_iboost_tax_ratio - ivop_boost_total) /
1334                    rc->pb_iboost_tax_ratio;
1335    
1336            DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- IFrame boost tax ratio:%.2f\n",
1337                            rc->pb_iboost_tax_ratio);
1338    
1339            /* Compute the average size of frames per frame type */
1340            for(i=0; i<3; i++) {
1341                    /* Special case for missing type or weird case */
1342                    if (rc->count[i] == 0 || rc->pb_iboost_tax_ratio == 0) {
1343                            rc->avg_length[i] = 1;
1344                    } else {
1345                            rc->avg_length[i] = rc->tot_scaled_length[i];
1346    
1347                            if (i == (XVID_TYPE_IVOP-1)) {
1348                                    /* I Frames total has to be added the boost total */
1349                                    rc->avg_length[i] += ivop_boost_total;
1350                            } else {
1351                                    /* P/B frames has to taxed */
1352                                    rc->avg_length[i] *= rc->pb_iboost_tax_ratio;
1353                            }
1354    
1355                            /* Finally compute the average frame size */
1356                            rc->avg_length[i] /= (double)rc->count[i];
1357                    }
1358            }
1359    
1360            /* Assymetric curve compression */
1361            if (rc->param.curve_compression_high || rc->param.curve_compression_low) {
1362                    double symetric_total;
1363                    double assymetric_delta_total;
1364    
1365                    /* Like I frame boosting, assymetric curve compression modifies the total
1366                     * amount of needed bits, we must compute the ratio so we can prescale
1367                     lengths */
1368                    symetric_total = 0;
1369                    assymetric_delta_total = 0;
1370                    for (i=0; i<rc->num_frames; i++) {
1371                            double assymetric_delta;
1372                            double dbytes;
1373                            twopass_stat_t * s = &rc->stats[i];
1374    
1375                            /* I Frames are not concerned by assymetric scaling */
1376                            if (s->type == XVID_TYPE_IVOP)
1377                                    continue;
1378    
1379                            /* During the real run, we would have to apply the iboost tax */
1380                            dbytes = s->scaled_length * rc->pb_iboost_tax_ratio;
1381    
1382                            /* Update the symmetric curve compression total */
1383                            symetric_total += dbytes;
1384    
1385                            /* Apply assymetric curve compression */
1386                            if (dbytes > rc->avg_length[s->type-1])
1387                                    assymetric_delta = (rc->avg_length[s->type-1] - dbytes) * (double)rc->param.curve_compression_high / 100.0f;
1388                            else
1389                                    assymetric_delta = (rc->avg_length[s->type-1] - dbytes) * (double)rc->param.curve_compression_low  / 100.0f;
1390    
1391                            /* Cap to the minimum frame size if needed */
1392                            if (dbytes + assymetric_delta < rc->min_length[s->type-1])
1393                                    assymetric_delta = rc->min_length[s->type-1] - dbytes;
1394    
1395                            /* Accumulate after assymetric curve compression */
1396                            assymetric_delta_total += assymetric_delta;
1397                    }
1398    
1399                    /* Compute the tax that all p/b frames have to pay in order to respect the
1400                     * bit distribution changes that the assymetric compression curve imposes
1401                     * We want assymetric_total = sum(0, n-1, tax.scaled_length)
1402                     *      ie assymetric_total = ratio.sum(0, n-1, scaled_length)
1403                     *         ratio = assymetric_total / symmetric_total */
1404                    rc->assymetric_tax_ratio = ((double)symetric_total - (double)assymetric_delta_total) / (double)symetric_total;
1405            } else {
1406                    rc->assymetric_tax_ratio = 1.0f;
1407            }
1408    
1409            DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- Assymetric tax ratio:%.2f\n", rc->assymetric_tax_ratio);
1410    
1411            /* Last bits that need to be reset */
1412            rc->overflow = 0;
1413            rc->KFoverflow = 0;
1414            rc->KFoverflow_partial = 0;
1415            rc->KF_idx = 0;
1416            rc->desired_total = 0;
1417            rc->real_total = 0;
1418    
1419            /* Job done */
1420            return;
1421    }
1422    
1423    
1424    #ifdef VBV
1425    
1426    /*****************************************************************************
1427     * VBV compliancy check and scale
1428     * MPEG-4 standard specifies certain restrictions for bitrate/framesize in VBR
1429     * to enable playback on devices with limited readspeed and memory (and which
1430     * aren't...)
1431     *
1432     * DivX profiles have 2 criteria: VBV as in MPEG standard
1433     *                                a limit on peak bitrate for any 3 seconds
1434     *
1435     * But if VBV is fulfilled, peakrate is automatically fulfilled in any profile
1436     * define so far, so we check for it (for completeness) but correct only VBV
1437     *
1438     *****************************************************************************/
1439    
1440    #define VBV_COMPLIANT 0
1441    #define VBV_UNDERFLOW 1 /* video buffer runs empty */
1442    #define VBV_OVERFLOW 2  /* doesn't exist for VBR encoding */
1443    #define VBV_PEAKRATE 4  /* peak bitrate (within 3s) violated */
1444    
1445    static int check_curve_for_vbv_compliancy(rc_2pass2_t * rc, const float fps)
1446    {
1447    /* We do all calculations in float, for higher accuracy, and bytes for convenience
1448    
1449       typical values from DivX Home Theater profile:
1450       vbvsize= 384*1024 (384kB), vbvinitial= 288*1024 (75% fill)
1451       maxrate= 4000000 (4MBps), peakrate= 10000000 (10MBps)
1452    
1453       PAL: offset3s = 75 (3 seconds of 25fps)
1454       NTSC: offset3s = 90 (3 seconds of 29.97fps) or 72 (3 seconds of 23.976fps)
1455    */
1456    
1457      const float vbvsize = (float)rc->param.vbvsize/8.f;
1458      float vbvfill = (float)rc->param.vbvinitial/8.f;
1459    
1460      const float maxrate = (float)rc->param.vbv_maxrate;
1461      const float peakrate = (float)rc->param.vbv_peakrate;
1462      const float r0 = (int)(maxrate/fps+0.5)/8.f;
1463    
1464      int bytes3s = 0;
1465      int offset3s = (int)(3.f*fps+0.5);
1466    
1467      int i;
1468      for (i=0; i<rc->num_frames; i++) {
1469    /* DivX 3s peak bitrate check  */
1470    
1471        bytes3s += rc->stats[i].scaled_length;
1472        if (i>=offset3s)
1473          bytes3s -= rc->stats[i-offset3s].scaled_length;
1474    
1475        if (8.f*bytes3s > 3*peakrate)
1476          return VBV_PEAKRATE;
1477    
1478    /* update vbv fill level */
1479    
1480        vbvfill += r0 - rc->stats[i].scaled_length;
1481    
1482    /* this check is _NOT_ an "overflow"! only reading from disk stops then */
1483        if (vbvfill > vbvsize)
1484          vbvfill = vbvsize;
1485    
1486    /* but THIS would be an underflow. report it! */
1487        if (vbvfill < 0)
1488          return VBV_UNDERFLOW;
1489      }
1490    
1491      return VBV_COMPLIANT;
1492    }
1493    /* idea: min(vbvfill) could be stored to print "minimum buffer fill" */
1494    
1495    
1496    
1497    static int scale_curve_for_vbv_compliancy(rc_2pass2_t * rc, const float fps)
1498    {
1499    /* correct any VBV violations. Peak bitrate violations disappears
1500       by this automatically
1501    
1502       This implementation follows
1503    
1504       Westerink, Rajagopalan, Gonzales "Two-pass MPEG-2 variable-bitrate encoding"
1505       IBM J. RES. DEVELOP. VOL 43, No. 4, July 1999, p.471--488
1506    
1507       Thanks, guys! This paper rocks!!!
1508    */
1509    
1510    /*
1511        For each scene of len N, we have to check up to N^2 possible buffer fills.
1512        This works well with MPEG-2 where N==12 or so, but for MPEG-4 it's a
1513        little slow...
1514    */
1515      const float vbvsize = (float)rc->param.vbvsize/8.f;
1516      const float vbvinitial = (float)rc->param.vbvinitial/8.f;
1517    
1518      const float maxrate = 0.9*rc->param.vbv_maxrate;
1519      const float vbvlow = 0.10f*vbvsize;
1520      const float r0 = (int)(maxrate/fps+0.5)/8.f;
1521    
1522      int i,k,l,n,violation = 0;
1523      float *scenefactor;
1524      int *scenestart;
1525      int *scenelength;
1526    
1527    /* first step: determine how many "scenes" there are and store their boundaries
1528       we could get all this from existing keyframe_positions, somehow, but there we
1529       don't have a min_scenelength, and it's no big deal to get it again.  */
1530    
1531      const int min_scenelength = 50;
1532      int num_scenes = 0;
1533      int last_scene = -999;
1534      for (i=0; i<rc->num_frames; i++) {
1535        if ( (rc->stats[i].type == XVID_TYPE_IVOP) && (i-last_scene>min_scenelength) )
1536        {
1537          last_scene = i;
1538          num_scenes++;
1539        }
1540      }
1541    
1542      scenefactor = (float*)malloc( num_scenes*sizeof(float) );
1543      scenestart = (int*)malloc( num_scenes*sizeof(int) );
1544      scenelength = (int*)malloc( num_scenes*sizeof(int) );
1545    
1546      if ((!scenefactor) || (!scenestart) || (!scenelength) )
1547      {
1548        free(scenefactor);
1549        free(scenestart);
1550        free(scenelength);
1551        /* remember: free(0) is valid and does exactly nothing. */
1552        return -1;
1553      }
1554    
1555    /* count again and safe the length/position */
1556    
1557      num_scenes = 0;
1558      last_scene = -999;
1559      for (i=0; i<rc->num_frames; i++) {
1560        if ( (rc->stats[i].type == XVID_TYPE_IVOP) && (i-last_scene>min_scenelength) )
1561        {
1562          if (num_scenes>0)
1563            scenelength[num_scenes-1]=i-last_scene;
1564          scenestart[num_scenes]=i;
1565          num_scenes++;
1566          last_scene = i;
1567        }
1568      }
1569      scenelength[num_scenes-1]=i-last_scene;
1570    
1571    /* second step: check for each scene, how much we can scale its frames up or down
1572       such that the VBV restriction is just fulfilled
1573    */
1574    
1575    
1576    #define R(k,n) (((n)+1-(k))*r0)     /* how much enters the buffer between frame k and n */
1577      for (l=0; l<num_scenes;l++)
1578      {
1579        const int start = scenestart[l];
1580        const int length = scenelength[l];
1581        twopass_stat_t * frames = &rc->stats[start];
1582    
1583        float S0n,Skn;
1584        float f,minf = 99999.f;
1585    
1586        S0n=0.;
1587        for (n=0;n<=length-1;n++)
1588        {
1589          S0n += frames[n].scaled_length;
1590    
1591          k=0;
1592          Skn = S0n;
1593          f = (R(k,n-1) + (vbvinitial - vbvlow)) / Skn;
1594          if (f < minf)
1595            minf = f;
1596    
1597          for (k=1;k<=n;k++)
1598          {
1599            Skn -= frames[k].scaled_length;
1600    
1601            f = (R(k,n-1) + (vbvsize - vbvlow)) / Skn;
1602            if (f < minf)
1603              minf = f;
1604          }
1605        }
1606    
1607        /* special case: at the end, fill buffer up to vbvinitial again
1608           TODO: Allow other values for buffer fill between scenes
1609           e.g. if n=N is smallest f-value, then check for better value */
1610    
1611        n=length;
1612        k=0;
1613        Skn = S0n;
1614        f = R(k,n-1)/Skn;
1615        if (f < minf)
1616          minf = f;
1617    
1618        for (k=1;k<=n-1;k++)
1619        {
1620          Skn -= frames[k].scaled_length;
1621    
1622          f = (R(k,n-1) + (vbvinitial - vbvlow)) / Skn;
1623          if (f < minf)
1624            minf = f;
1625        }
1626    
1627    #ifdef VBV_DEBUG
1628        printf("Scene %d (Frames %d-%d): VBVfactor %f\n", l, start, start+length-1 , minf);
1629    #endif
1630    
1631        scenefactor[l] = minf;
1632      }
1633    #undef R
1634    
1635    /* last step: now we know of any scene how much it can be scaled up or down without
1636       violating VBV. Next, distribute bits from the evil scenes to the good ones */
1637    
1638      do
1639      {
1640        float S_red = 0.f;    /* how much to redistribute */
1641        float S_elig = 0.f;   /* sum of bit for those scenes you can still swallow something*/
1642        int l;
1643    
1644        for (l=0;l<num_scenes;l++)   /* check how much is wrong */
1645        {
1646        const int start = scenestart[l];
1647        const int length = scenelength[l];
1648        twopass_stat_t * frames = &rc->stats[start];
1649    
1650          if (scenefactor[l] == 1.) /* exactly 1 means "don't touch this anymore!" */
1651            continue;
1652    
1653          if (scenefactor[l] > 1.) /* within limits */
1654          {
1655            for (n= 0; n < length; n++)
1656              S_elig += frames[n].scaled_length;
1657          }
1658          else /* underflowing segment */
1659          {
1660            for (n= 0; n < length; n++)
1661            {
1662              float newbytes = (float)frames[n].scaled_length * scenefactor[l];
1663              S_red += (float)frames[n].scaled_length - (float)newbytes;
1664              frames[n].scaled_length =(int)newbytes;
1665            }
1666            scenefactor[l] = 1.f;
1667          }
1668        }
1669    
1670        if (S_red < 1.f)   /* no more underflows */
1671          break;
1672    
1673        if (S_elig < 1.f)
1674        {
1675    #ifdef VBV_DEBUG
1676          fprintf(stderr,"Everything underflowing. \n");
1677    #endif
1678          free(scenefactor);
1679          free(scenestart);
1680          free(scenelength);
1681          return -2;
1682        }
1683    
1684        const float f_red = (1.f + S_red/S_elig);
1685    
1686    #ifdef VBV_DEBUG
1687        printf("Moving %.0f kB to avoid buffer underflow, correction factor: %.5f\n",S_red/1024.f,f_red);
1688    #endif
1689    
1690        violation=0;
1691        for (l=0; l<num_scenes; l++)   /* scale remaining scenes up to meet total size */
1692        {
1693          const int start = scenestart[l];
1694          const int length = scenelength[l];
1695          twopass_stat_t * frames = &rc->stats[start];
1696    
1697          if (scenefactor[l] == 1.)
1698            continue;
1699    
1700          /* there shouldn't be any segments with factor<1 left, so all the rest is >1 */
1701    
1702          for (n= 0; n < length; n++)
1703          {
1704            frames[n].scaled_length = (int)(frames[n].scaled_length * f_red + 0.5);
1705          }
1706    
1707          scenefactor[l] /= f_red;
1708          if (scenefactor[l] < 1.f)
1709            violation=1;
1710        }
1711    
1712      } while (violation);
1713    
1714      free(scenefactor);
1715      free(scenestart);
1716      free(scenelength);
1717      return 0;
1718    }
1719    
1720    
1721    #endif
1722    
1723    
1724    /*****************************************************************************
1725     * Still more low level stuff (nothing to do with stats treatment)
1726     ****************************************************************************/
1727    
1728    /* This function returns an allocated string containing a complete line read
1729     * from the file starting at the current position */
1730    static char *
1731    readline(FILE *f)
1732    {
1733            char *buffer = NULL;
1734            int buffer_size = 0;
1735            int pos = 0;
1736    
1737            do {
1738                    int c;
1739    
1740                    /* Read a character from the stream */
1741                    c = fgetc(f);
1742    
1743                    /* Is that EOF or new line ? */
1744                    if(c == EOF || c == '\n')
1745                            break;
1746    
1747                    /* Do we have to update buffer ? */
1748                    if(pos >= buffer_size - 1) {
1749                            buffer_size += BUF_SZ;
1750                            buffer = (char*)realloc(buffer, buffer_size);
1751                            if (buffer == NULL)
1752                                    return(NULL);
1753                    }
1754    
1755                    buffer[pos] = c;
1756                    pos++;
1757            } while(1);
1758    
1759            /* Read \n or EOF */
1760            if (buffer == NULL) {
1761                    /* EOF, so we reached the end of the file, return NULL */
1762                    if(feof(f))
1763                            return(NULL);
1764    
1765                    /* Just an empty line with just a newline, allocate a 1 byte buffer to
1766                     * store a zero length string */
1767                    buffer = (char*)malloc(1);
1768                    if(buffer == NULL)
1769                            return(NULL);
1770            }
1771    
1772            /* Zero terminated string */
1773            buffer[pos] = '\0';
1774    
1775            return(buffer);
1776    }
1777    
1778    /* This function returns a pointer to the first non space char in the given
1779     * string */
1780    static char *
1781    skipspaces(char *string)
1782    {
1783            const char spaces[] =
1784                    {
1785                            ' ','\t','\0'
1786                    };
1787            const char *spacechar = spaces;
1788    
1789            if (string == NULL) return(NULL);
1790    
1791            while (*string != '\0') {
1792                    /* Test against space chars */
1793                    while (*spacechar != '\0') {
1794                            if (*string == *spacechar) {
1795                                    string++;
1796                                    spacechar = spaces;
1797                                    break;
1798                            }
1799                            spacechar++;
1800                    }
1801    
1802                    /* No space char */
1803                    if (*spacechar == '\0') return(string);
1804            }
1805    
1806            return(string);
1807    }
1808    
1809    /* This function returns a boolean that tells if the string is only a
1810     * comment */
1811    static int
1812    iscomment(char *string)
1813    {
1814            const char comments[] =
1815                    {
1816                            '#',';', '%', '\0'
1817                    };
1818            const char *cmtchar = comments;
1819            int iscomment = 0;
1820    
1821            if (string == NULL) return(1);
1822    
1823            string = skipspaces(string);
1824    
1825            while(*cmtchar != '\0') {
1826                    if(*string == *cmtchar) {
1827                            iscomment = 1;
1828                            break;
1829                    }
1830                    cmtchar++;
1831            }
1832    
1833            return(iscomment);
1834    }
1835    
1836    #if 0
1837    static void
1838    stats_print(rc_2pass2_t * rc)
1839    {
1840            int i;
1841            const char frame_type[4] = { 'i', 'p', 'b', 's'};
1842    
1843            for (i=0; i<rc->num_frames; i++) {
1844                    twopass_stat_t *s = &rc->stats[i];
1845                    DPRINTF(XVID_DEBUG_RC, "[xvid rc] -- frame:%d type:%c quant:%d stats:%d scaled:%d desired:%d actual:%d overflow(%c):%.2f\n",
1846                                    i, frame_type[s->type-1], -1, s->length, s->scaled_length,
1847                                    s->desired_length, -1, frame_type[s->type-1], -1.0f);
1848            }
1849    }
1850    #endif

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

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