[svn] / trunk / xvidcore / src / utils / ratecontrol.c Repository:
ViewVC logotype

Annotation of /trunk/xvidcore/src/utils/ratecontrol.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 221 - (view) (download)

1 : edgomez 221 /*****************************************************************************
2 :     *
3 :     * XVID MPEG-4 VIDEO CODEC
4 :     * - Rate Controler module -
5 :     *
6 :     * This program is free software ; you can redistribute it and/or modify
7 :     * it under the terms of the GNU General Public License as published by
8 :     * the Free Software Foundation ; either version 2 of the License, or
9 :     * (at your option) any later version.
10 :     *
11 :     * This program is distributed in the hope that it will be useful,
12 :     * but WITHOUT ANY WARRANTY ; without even the implied warranty of
13 :     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 :     * GNU General Public License for more details.
15 :     *
16 :     * You should have received a copy of the GNU General Public License
17 :     * along with this program ; if not, write to the Free Software
18 :     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 :     *
20 :     ****************************************************************************/
21 :     /*****************************************************************************
22 :     *
23 :     * History
24 :     *
25 :     * - Mon Jun 17 13:04:15 2002 Added legal header
26 :     *
27 :     * $Id: ratecontrol.c,v 1.13 2002-06-17 12:23:32 edgomez Exp $
28 :     *
29 :     ****************************************************************************/
30 :    
31 :    
32 :     /* For the fabs function */
33 :     #include <math.h>
34 :    
35 :     #include "ratecontrol.h"
36 :    
37 :     /*****************************************************************************
38 :     * Local prototype - defined at the end of the file
39 :     ****************************************************************************/
40 :    
41 :     static int get_initial_quant(int bpp);
42 :    
43 :     /*****************************************************************************
44 :     * RateControlInit
45 :     *
46 :     * This function initialize the structure members of 'rate_control' argument
47 :     * according to the other arguments.
48 :     *
49 :     * Returned value : None
50 :     *
51 :     ****************************************************************************/
52 :    
53 :     void
54 :     RateControlInit(RateControl * rate_control,
55 :     uint32_t target_rate,
56 :     uint32_t reaction_delay_factor,
57 :     uint32_t averaging_period,
58 :     uint32_t buffer,
59 :     int framerate,
60 :     int max_quant,
61 :     int min_quant)
62 :     {
63 :     int i;
64 :    
65 :     rate_control->frames = 0;
66 :     rate_control->total_size = 0;
67 :    
68 :     rate_control->framerate = framerate / 1000.0;
69 :     rate_control->target_rate = target_rate;
70 :    
71 :     rate_control->rtn_quant = get_initial_quant(0);
72 :     rate_control->max_quant = max_quant;
73 :     rate_control->min_quant = min_quant;
74 :    
75 :     for (i = 0; i < 32; ++i) {
76 :     rate_control->quant_error[i] = 0.0;
77 :     }
78 :    
79 :     rate_control->target_framesize =
80 :     (double) target_rate / 8.0 / rate_control->framerate;
81 :     rate_control->sequence_quality = 2.0 / (double) rate_control->rtn_quant;
82 :     rate_control->avg_framesize = rate_control->target_framesize;
83 :    
84 :     rate_control->reaction_delay_factor = reaction_delay_factor;
85 :     rate_control->averaging_period = averaging_period;
86 :     rate_control->buffer = buffer;
87 :     }
88 :    
89 :     /*****************************************************************************
90 :     * RateControlGetQ
91 :     *
92 :     * Returned value : - the current 'rate_control' quantizer value
93 :     *
94 :     ****************************************************************************/
95 :    
96 :     int
97 :     RateControlGetQ(RateControl * rate_control,
98 :     int keyframe)
99 :     {
100 :     return rate_control->rtn_quant;
101 :     }
102 :    
103 :     /*****************************************************************************
104 :     * RateControlUpdate
105 :     *
106 :     * This function is called once a coded frame to update all internal
107 :     * parameters of 'rate_control'.
108 :     *
109 :     * Returned value : None
110 :     *
111 :     ****************************************************************************/
112 :    
113 :     void
114 :     RateControlUpdate(RateControl * rate_control,
115 :     int16_t quant,
116 :     int frame_size,
117 :     int keyframe)
118 :     {
119 :     int64_t deviation;
120 :     double overflow, averaging_period, reaction_delay_factor;
121 :     double quality_scale, base_quality, target_quality;
122 :     int32_t rtn_quant;
123 :    
124 :     rate_control->frames++;
125 :     rate_control->total_size += frame_size;
126 :    
127 :     deviation =
128 :     (int64_t) ((double) rate_control->total_size -
129 :     ((double)
130 :     ((double) rate_control->target_rate / 8.0 /
131 :     (double) rate_control->framerate) *
132 :     (double) rate_control->frames));
133 :    
134 :     DEBUGCBR((int32_t) (rate_control->frames - 1), rate_control->rtn_quant,
135 :     (int32_t) deviation);
136 :    
137 :     if (rate_control->rtn_quant >= 2) {
138 :     averaging_period = (double) rate_control->averaging_period;
139 :     rate_control->sequence_quality -=
140 :     rate_control->sequence_quality / averaging_period;
141 :     rate_control->sequence_quality +=
142 :     2.0 / (double) rate_control->rtn_quant / averaging_period;
143 :     if (rate_control->sequence_quality < 0.1)
144 :     rate_control->sequence_quality = 0.1;
145 :    
146 :     if (!keyframe) {
147 :     reaction_delay_factor =
148 :     (double) rate_control->reaction_delay_factor;
149 :     rate_control->avg_framesize -=
150 :     rate_control->avg_framesize / reaction_delay_factor;
151 :     rate_control->avg_framesize += frame_size / reaction_delay_factor;
152 :     }
153 :     }
154 :    
155 :     quality_scale =
156 :     rate_control->target_framesize / rate_control->avg_framesize *
157 :     rate_control->target_framesize / rate_control->avg_framesize;
158 :    
159 :     base_quality = rate_control->sequence_quality;
160 :     if (quality_scale >= 1.0) {
161 :     base_quality = 1.0 - (1.0 - base_quality) / quality_scale;
162 :     } else {
163 :     base_quality = 0.06452 + (base_quality - 0.06452) * quality_scale;
164 :     }
165 :    
166 :     overflow = -((double) deviation / (double) rate_control->buffer);
167 :    
168 :     target_quality =
169 :     base_quality + (base_quality -
170 :     0.06452) * overflow / rate_control->target_framesize;
171 :    
172 :     if (target_quality > 2.0)
173 :     target_quality = 2.0;
174 :     else if (target_quality < 0.06452)
175 :     target_quality = 0.06452;
176 :    
177 :     rtn_quant = (int32_t) (2.0 / target_quality);
178 :    
179 :     if (rtn_quant < 31) {
180 :     rate_control->quant_error[rtn_quant] +=
181 :     2.0 / target_quality - rtn_quant;
182 :     if (rate_control->quant_error[rtn_quant] >= 1.0) {
183 :     rate_control->quant_error[rtn_quant] -= 1.0;
184 :     rtn_quant++;
185 :     }
186 :     }
187 :    
188 :     if (rtn_quant > rate_control->rtn_quant + 1)
189 :     rtn_quant = rate_control->rtn_quant + 1;
190 :     else if (rtn_quant < rate_control->rtn_quant - 1)
191 :     rtn_quant = rate_control->rtn_quant - 1;
192 :    
193 :     if (rtn_quant > rate_control->max_quant)
194 :     rtn_quant = rate_control->max_quant;
195 :     else if (rtn_quant < rate_control->min_quant)
196 :     rtn_quant = rate_control->min_quant;
197 :    
198 :     rate_control->rtn_quant = rtn_quant;
199 :     }
200 :    
201 :     /*****************************************************************************
202 :     * Local functions
203 :     ****************************************************************************/
204 :    
205 :     static int
206 :     get_initial_quant(int bpp)
207 :     {
208 :     return 5;
209 :     }

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