[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 851 - (view) (download)

1 : edgomez 221 /*****************************************************************************
2 :     *
3 :     * XVID MPEG-4 VIDEO CODEC
4 :     * - Rate Controler module -
5 :     *
6 : edgomez 851 * 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 : edgomez 221 * (at your option) any later version.
10 :     *
11 :     * This program is distributed in the hope that it will be useful,
12 : edgomez 851 * but WITHOUT ANY WARRANTY ; without even the implied warranty of
13 : edgomez 221 * 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 : edgomez 851 * along with this program ; if not, write to the Free Software
18 : edgomez 221 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 :     *
20 : edgomez 851 ****************************************************************************/
21 :     /*****************************************************************************
22 : edgomez 221 *
23 : edgomez 851 * History
24 : edgomez 654 *
25 : edgomez 851 * - Mon Jun 17 13:04:15 2002 Added legal header
26 : edgomez 654 *
27 : edgomez 851 * $Id: ratecontrol.c,v 1.19 2003-02-15 15:22:19 edgomez Exp $
28 : edgomez 654 *
29 : edgomez 221 ****************************************************************************/
30 :    
31 : edgomez 851
32 :     /* For the fabs function */
33 : edgomez 221 #include <math.h>
34 : edgomez 851
35 : edgomez 221 #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 : edgomez 851 DPRINTF(DPRINTF_RC, "CBR: frame: %i, quant: %i, deviation: %i\n",
135 :     (int32_t)(rate_control->frames - 1),
136 :     rate_control->rtn_quant,
137 :     (int32_t) deviation);
138 : edgomez 221
139 :     if (rate_control->rtn_quant >= 2) {
140 :     averaging_period = (double) rate_control->averaging_period;
141 :     rate_control->sequence_quality -=
142 :     rate_control->sequence_quality / averaging_period;
143 :     rate_control->sequence_quality +=
144 :     2.0 / (double) rate_control->rtn_quant / averaging_period;
145 :     if (rate_control->sequence_quality < 0.1)
146 :     rate_control->sequence_quality = 0.1;
147 :    
148 :     if (!keyframe) {
149 :     reaction_delay_factor =
150 :     (double) rate_control->reaction_delay_factor;
151 :     rate_control->avg_framesize -=
152 :     rate_control->avg_framesize / reaction_delay_factor;
153 :     rate_control->avg_framesize += frame_size / reaction_delay_factor;
154 :     }
155 :     }
156 :    
157 :     quality_scale =
158 :     rate_control->target_framesize / rate_control->avg_framesize *
159 :     rate_control->target_framesize / rate_control->avg_framesize;
160 :    
161 :     base_quality = rate_control->sequence_quality;
162 :     if (quality_scale >= 1.0) {
163 :     base_quality = 1.0 - (1.0 - base_quality) / quality_scale;
164 :     } else {
165 :     base_quality = 0.06452 + (base_quality - 0.06452) * quality_scale;
166 :     }
167 :    
168 :     overflow = -((double) deviation / (double) rate_control->buffer);
169 :    
170 :     target_quality =
171 :     base_quality + (base_quality -
172 :     0.06452) * overflow / rate_control->target_framesize;
173 :    
174 :     if (target_quality > 2.0)
175 :     target_quality = 2.0;
176 :     else if (target_quality < 0.06452)
177 :     target_quality = 0.06452;
178 :    
179 :     rtn_quant = (int32_t) (2.0 / target_quality);
180 :    
181 :     if (rtn_quant < 31) {
182 :     rate_control->quant_error[rtn_quant] +=
183 :     2.0 / target_quality - rtn_quant;
184 :     if (rate_control->quant_error[rtn_quant] >= 1.0) {
185 :     rate_control->quant_error[rtn_quant] -= 1.0;
186 :     rtn_quant++;
187 :     }
188 :     }
189 :    
190 :     if (rtn_quant > rate_control->rtn_quant + 1)
191 :     rtn_quant = rate_control->rtn_quant + 1;
192 :     else if (rtn_quant < rate_control->rtn_quant - 1)
193 :     rtn_quant = rate_control->rtn_quant - 1;
194 :    
195 :     if (rtn_quant > rate_control->max_quant)
196 :     rtn_quant = rate_control->max_quant;
197 :     else if (rtn_quant < rate_control->min_quant)
198 :     rtn_quant = rate_control->min_quant;
199 :    
200 :     rate_control->rtn_quant = rtn_quant;
201 :     }
202 :    
203 :     /*****************************************************************************
204 :     * Local functions
205 :     ****************************************************************************/
206 :    
207 :     static int
208 :     get_initial_quant(int bpp)
209 :     {
210 :     return 5;
211 :     }

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