[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 967 - (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 : Isibaar 967 * $Id: ratecontrol.c,v 1.20 2003-04-04 03:16:09 Isibaar 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 : Isibaar 967 if(keyframe > 2)
125 :     return;
126 :    
127 : edgomez 221 rate_control->frames++;
128 :     rate_control->total_size += frame_size;
129 :    
130 : Isibaar 967 rate_control->rtn_quant = quant;
131 :    
132 : edgomez 221 deviation =
133 :     (int64_t) ((double) rate_control->total_size -
134 :     ((double)
135 :     ((double) rate_control->target_rate / 8.0 /
136 :     (double) rate_control->framerate) *
137 :     (double) rate_control->frames));
138 :    
139 : edgomez 851 DPRINTF(DPRINTF_RC, "CBR: frame: %i, quant: %i, deviation: %i\n",
140 :     (int32_t)(rate_control->frames - 1),
141 :     rate_control->rtn_quant,
142 :     (int32_t) deviation);
143 : edgomez 221
144 :     if (rate_control->rtn_quant >= 2) {
145 :     averaging_period = (double) rate_control->averaging_period;
146 :     rate_control->sequence_quality -=
147 :     rate_control->sequence_quality / averaging_period;
148 :     rate_control->sequence_quality +=
149 :     2.0 / (double) rate_control->rtn_quant / averaging_period;
150 :     if (rate_control->sequence_quality < 0.1)
151 :     rate_control->sequence_quality = 0.1;
152 :    
153 : Isibaar 967 if (keyframe != 1) {
154 : edgomez 221 reaction_delay_factor =
155 :     (double) rate_control->reaction_delay_factor;
156 :     rate_control->avg_framesize -=
157 :     rate_control->avg_framesize / reaction_delay_factor;
158 :     rate_control->avg_framesize += frame_size / reaction_delay_factor;
159 :     }
160 :     }
161 :    
162 :     quality_scale =
163 :     rate_control->target_framesize / rate_control->avg_framesize *
164 :     rate_control->target_framesize / rate_control->avg_framesize;
165 :    
166 :     base_quality = rate_control->sequence_quality;
167 :     if (quality_scale >= 1.0) {
168 :     base_quality = 1.0 - (1.0 - base_quality) / quality_scale;
169 :     } else {
170 :     base_quality = 0.06452 + (base_quality - 0.06452) * quality_scale;
171 :     }
172 :    
173 :     overflow = -((double) deviation / (double) rate_control->buffer);
174 :    
175 :     target_quality =
176 :     base_quality + (base_quality -
177 :     0.06452) * overflow / rate_control->target_framesize;
178 :    
179 :     if (target_quality > 2.0)
180 :     target_quality = 2.0;
181 :     else if (target_quality < 0.06452)
182 :     target_quality = 0.06452;
183 :    
184 :     rtn_quant = (int32_t) (2.0 / target_quality);
185 :    
186 :     if (rtn_quant < 31) {
187 :     rate_control->quant_error[rtn_quant] +=
188 :     2.0 / target_quality - rtn_quant;
189 :     if (rate_control->quant_error[rtn_quant] >= 1.0) {
190 :     rate_control->quant_error[rtn_quant] -= 1.0;
191 :     rtn_quant++;
192 :     }
193 :     }
194 :    
195 :     if (rtn_quant > rate_control->rtn_quant + 1)
196 :     rtn_quant = rate_control->rtn_quant + 1;
197 :     else if (rtn_quant < rate_control->rtn_quant - 1)
198 :     rtn_quant = rate_control->rtn_quant - 1;
199 :    
200 :     if (rtn_quant > rate_control->max_quant)
201 :     rtn_quant = rate_control->max_quant;
202 :     else if (rtn_quant < rate_control->min_quant)
203 :     rtn_quant = rate_control->min_quant;
204 :    
205 :     rate_control->rtn_quant = rtn_quant;
206 :     }
207 :    
208 :     /*****************************************************************************
209 :     * Local functions
210 :     ****************************************************************************/
211 :    
212 :     static int
213 :     get_initial_quant(int bpp)
214 :     {
215 : Isibaar 967 return 8;
216 : edgomez 221 }

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