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

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

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

revision 1.1, Mon May 12 12:33:16 2003 UTC revision 1.1.2.4, Thu Jul 24 12:00:06 2003 UTC
# Line 0  Line 1 
1    /*****************************************************************************
2     *
3     *  XviD Standard Plugins
4     *  - single-pass bitrate controller implementation -
5     *
6     *  Copyright(C) 2002      Benjamin Lambert <foxer@hotmail.com>
7     *               2002-2003 Edouard Gomez <ed.gomez@free.fr>
8     *
9     *  This program is free software; you can redistribute it and/or modify
10     *  it under the terms of the GNU General Public License as published by
11     *  the Free Software Foundation; either version 2 of the License, or
12     *  (at your option) any later version.
13     *
14     *  This program is distributed in the hope that it will be useful,
15     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     *  GNU General Public License for more details.
18     *
19     *  You should have received a copy of the GNU General Public License
20     *  along with this program; if not, write to the Free Software
21     *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22     *
23     * $Id$
24     *
25     ****************************************************************************/
26    
27    
28    #include <limits.h>
29    
30    #include "../xvid.h"
31    #include "../image/image.h"
32    
33    #define DEFAULT_INITIAL_QUANTIZER 8
34    
35    #define DEFAULT_BITRATE 900000  /* 900kbps */
36    #define DEFAULT_DELAY_FACTOR 16
37    #define DEFAULT_AVERAGING_PERIOD 100
38    #define DEFAULT_BUFFER 100
39    
40    typedef struct
41    {
42            int reaction_delay_factor;
43            int averaging_period;
44            int buffer;
45    
46            int bytes_per_sec;
47            double target_framesize;
48    
49            double time;
50            int64_t total_size;
51            int rtn_quant;
52    
53            double sequence_quality;
54            double avg_framesize;
55            double quant_error[31];
56    
57        double fq_error;
58    }
59    rc_single_t;
60    
61    
62    static int
63    get_initial_quant(unsigned int bitrate)
64    {
65    
66    #if defined(DEFAULT_INITIAL_QUANTIZER)
67        return (DEFAULT_INITIAL_QUANTIZER);
68    #else
69            int i;
70    
71            const unsigned int bitrate_quant[31] = {
72                    UINT_MAX
73            };
74    
75            for (i = 30; i >= 0; i--) {
76                    if (bitrate > bitrate_quant[i])
77                            continue;
78            }
79    
80            return (i + 1);
81    #endif
82    }
83    
84    static int
85    rc_single_create(xvid_plg_create_t * create,
86                              rc_single_t ** handle)
87    {
88            xvid_plugin_single_t *param = (xvid_plugin_single_t *) create->param;
89            rc_single_t *rc;
90            int i;
91    
92            /*
93             * single needs to caclculate the average frame size. In order to do that,
94             * we really need valid fps
95             */
96            if (create->fincr == 0) {
97                    return XVID_ERR_FAIL;
98            }
99    
100            /* Allocate context struct */
101            if ((rc = malloc(sizeof(rc_single_t))) == NULL)
102                    return (XVID_ERR_MEMORY);
103    
104            /* Constants */
105            rc->bytes_per_sec =     (param->bitrate > 0) ? param->bitrate / 8 : DEFAULT_BITRATE / 8;
106            rc->target_framesize =(double) rc->bytes_per_sec / ((double) create->fbase / create->fincr);
107            rc->reaction_delay_factor =     (param->reaction_delay_factor > 0) ? param->reaction_delay_factor : DEFAULT_DELAY_FACTOR;
108            rc->averaging_period = (param->averaging_period > 0) ? param->averaging_period : DEFAULT_AVERAGING_PERIOD;
109            rc->buffer = (param->buffer > 0) ? param->buffer : DEFAULT_BUFFER;
110    
111            rc->time = 0;
112            rc->total_size = 0;
113            rc->rtn_quant = get_initial_quant(param->bitrate);
114    
115            /* Reset quant error accumulators */
116            for (i = 0; i < 31; i++)
117                    rc->quant_error[i] = 0.0;
118    
119            /* Last bunch of variables */
120            rc->sequence_quality = 2.0 / (double) rc->rtn_quant;
121            rc->avg_framesize = rc->target_framesize;
122    
123        rc->fq_error = 0;
124    
125            /* Bind the RC */
126            *handle = rc;
127    
128            /* A bit of debug info */
129            DPRINTF(XVID_DEBUG_RC, "bytes_per_sec: %i\n", rc->bytes_per_sec);
130            DPRINTF(XVID_DEBUG_RC, "frame rate   : %f\n", (double) create->fbase / create->fincr);
131            DPRINTF(XVID_DEBUG_RC, "target_framesize: %f\n", rc->target_framesize);
132    
133            return (0);
134    }
135    
136    
137    static int
138    rc_single_destroy(rc_single_t * rc,
139                               xvid_plg_destroy_t * destroy)
140    {
141            free(rc);
142            return (0);
143    }
144    
145    
146    static int
147    rc_single_before(rc_single_t * rc,
148                              xvid_plg_data_t * data)
149    {
150         if (data->quant <= 0) {
151            if (data->zone && data->zone->mode == XVID_ZONE_QUANT) {
152                rc->fq_error += (double)data->zone->increment / (double)data->zone->base;
153                data->quant = (int)rc->fq_error;
154                rc->fq_error -= data->quant;
155            }else {
156                    data->quant = rc->rtn_quant;
157            }
158        }
159            return 0;
160    }
161    
162    
163    static int
164    rc_single_after(rc_single_t * rc,
165                             xvid_plg_data_t * data)
166    {
167            int64_t deviation;
168            int rtn_quant;
169            double overflow;
170            double averaging_period;
171            double reaction_delay_factor;
172            double quality_scale;
173            double base_quality;
174            double target_quality;
175    
176            /* Update internal values */
177            rc->time += (double) data->fincr / data->fbase;
178            rc->total_size += data->length;
179    
180            if(data->type == XVID_TYPE_BVOP)
181                    return (0);
182    
183            rc->rtn_quant = data->quant;
184    
185            /* Compute the deviation from expected total size */
186            deviation = (int64_t)
187                    ((double) rc->total_size - (double) rc->bytes_per_sec * rc->time);
188    
189    
190            if (data->quant >= 2) {
191    
192                    averaging_period = (double) rc->averaging_period;
193    
194                    rc->sequence_quality -= rc->sequence_quality / averaging_period;
195    
196                    rc->sequence_quality +=
197                            2.0 / (double) data->quant / averaging_period;
198    
199                    if (rc->sequence_quality < 0.1)
200                            rc->sequence_quality = 0.1;
201    
202                    if (data->type != XVID_TYPE_IVOP) {
203                            reaction_delay_factor = (double) rc->reaction_delay_factor;
204                            rc->avg_framesize -= rc->avg_framesize / reaction_delay_factor;
205                            rc->avg_framesize += data->length / reaction_delay_factor;
206                    }
207    
208            }
209    
210            quality_scale =
211                    rc->target_framesize / rc->avg_framesize * rc->target_framesize /
212                    rc->avg_framesize;
213    
214            base_quality = rc->sequence_quality;
215            if (quality_scale >= 1.0) {
216                    base_quality = 1.0 - (1.0 - base_quality) / quality_scale;
217            } else {
218                    base_quality = 0.06452 + (base_quality - 0.06452) * quality_scale;
219            }
220    
221            overflow = -((double) deviation / (double) rc->buffer);
222    
223            target_quality =
224                    base_quality + (base_quality -
225                                                    0.06452) * overflow / rc->target_framesize;
226    
227            if (target_quality > 2.0)
228                    target_quality = 2.0;
229            else if (target_quality < 0.06452)
230                    target_quality = 0.06452;
231    
232            rtn_quant = (int) (2.0 / target_quality);
233    
234            if (rtn_quant > 0 && rtn_quant < 31) {
235                    rc->quant_error[rtn_quant - 1] += 2.0 / target_quality - rtn_quant;
236                    if (rc->quant_error[rtn_quant - 1] >= 1.0) {
237                            rc->quant_error[rtn_quant - 1] -= 1.0;
238                            rtn_quant++;
239                    }
240            }
241    
242        /* prevent rapid quantization change */
243            if (rtn_quant > data->quant + 1)
244                    rtn_quant = data->quant + 1;
245            else if (rtn_quant < data->quant - 1)
246                    rtn_quant = data->quant - 1;
247    
248        /* limit to min/max range */
249            if (rtn_quant > data->max_quant[data->type-1])
250                    rtn_quant = data->max_quant[data->type-1];
251            else if (rtn_quant < data->min_quant[data->type-1])
252                    rtn_quant = data->min_quant[data->type-1];
253    
254            rc->rtn_quant = rtn_quant;
255    
256            return (0);
257    }
258    
259    
260    
261    int
262    xvid_plugin_single(void *handle,
263                                    int opt,
264                                    void *param1,
265                                    void *param2)
266    {
267            switch (opt) {
268            case XVID_PLG_INFO:
269                    return 0;
270    
271            case XVID_PLG_CREATE:
272                    return rc_single_create((xvid_plg_create_t *) param1, param2);
273    
274            case XVID_PLG_DESTROY:
275                    return rc_single_destroy((rc_single_t *) handle,(xvid_plg_destroy_t *) param1);
276    
277            case XVID_PLG_BEFORE:
278                    return rc_single_before((rc_single_t *) handle, (xvid_plg_data_t *) param1);
279    
280            case XVID_PLG_AFTER:
281                    return rc_single_after((rc_single_t *) handle, (xvid_plg_data_t *) param1);
282            }
283    
284            return XVID_ERR_FAIL;
285    }

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