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

1 : edgomez 221 /*****************************************************************************
2 :     *
3 :     * XVID MPEG-4 VIDEO CODEC
4 :     * - Rate Controler module -
5 :     *
6 : h 542 * Copyright(C) 2002 Benjamin Lambert <foxer@hotmail.com>
7 : edgomez 423 *
8 : edgomez 654 * This file is part of XviD, a free MPEG-4 video encoder/decoder
9 : edgomez 423 *
10 : edgomez 654 * XviD is free software; you can redistribute it and/or modify it
11 :     * under the terms of the GNU General Public License as published by
12 : edgomez 423 * the Free Software Foundation; either version 2 of the License, or
13 : edgomez 221 * (at your option) any later version.
14 :     *
15 :     * This program is distributed in the hope that it will be useful,
16 : edgomez 423 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 : edgomez 221 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 :     * GNU General Public License for more details.
19 :     *
20 :     * You should have received a copy of the GNU General Public License
21 : edgomez 423 * along with this program; if not, write to the Free Software
22 : edgomez 221 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 :     *
24 : edgomez 654 * Under section 8 of the GNU General Public License, the copyright
25 :     * holders of XVID explicitly forbid distribution in the following
26 :     * countries:
27 : edgomez 221 *
28 : edgomez 654 * - Japan
29 :     * - United States of America
30 :     *
31 :     * Linking XviD statically or dynamically with other modules is making a
32 :     * combined work based on XviD. Thus, the terms and conditions of the
33 :     * GNU General Public License cover the whole combination.
34 :     *
35 :     * As a special exception, the copyright holders of XviD give you
36 :     * permission to link XviD with independent modules that communicate with
37 :     * XviD solely through the VFW1.1 and DShow interfaces, regardless of the
38 :     * license terms of these independent modules, and to copy and distribute
39 :     * the resulting combined work under terms of your choice, provided that
40 :     * every copy of the combined work is accompanied by a complete copy of
41 :     * the source code of XviD (the version of XviD used to produce the
42 :     * combined work), being distributed under the terms of the GNU General
43 :     * Public License plus this exception. An independent module is a module
44 :     * which is not derived from or based on XviD.
45 :     *
46 :     * Note that people who make modified versions of XviD are not obligated
47 :     * to grant this special exception for their modified versions; it is
48 :     * their choice whether to do so. The GNU General Public License gives
49 :     * permission to release a modified version without this exception; this
50 :     * exception also makes it possible to release a modified version which
51 :     * carries forward this exception.
52 :     *
53 :     * $Id: ratecontrol.c,v 1.18 2002-11-17 00:51:11 edgomez Exp $
54 :     *
55 : edgomez 221 ****************************************************************************/
56 :    
57 :     #include <math.h>
58 :     #include "ratecontrol.h"
59 :    
60 :     /*****************************************************************************
61 :     * Local prototype - defined at the end of the file
62 :     ****************************************************************************/
63 :    
64 :     static int get_initial_quant(int bpp);
65 :    
66 :     /*****************************************************************************
67 :     * RateControlInit
68 :     *
69 :     * This function initialize the structure members of 'rate_control' argument
70 :     * according to the other arguments.
71 :     *
72 :     * Returned value : None
73 :     *
74 :     ****************************************************************************/
75 :    
76 :     void
77 :     RateControlInit(RateControl * rate_control,
78 :     uint32_t target_rate,
79 :     uint32_t reaction_delay_factor,
80 :     uint32_t averaging_period,
81 :     uint32_t buffer,
82 :     int framerate,
83 :     int max_quant,
84 :     int min_quant)
85 :     {
86 :     int i;
87 :    
88 :     rate_control->frames = 0;
89 :     rate_control->total_size = 0;
90 :    
91 :     rate_control->framerate = framerate / 1000.0;
92 :     rate_control->target_rate = target_rate;
93 :    
94 :     rate_control->rtn_quant = get_initial_quant(0);
95 :     rate_control->max_quant = max_quant;
96 :     rate_control->min_quant = min_quant;
97 :    
98 :     for (i = 0; i < 32; ++i) {
99 :     rate_control->quant_error[i] = 0.0;
100 :     }
101 :    
102 :     rate_control->target_framesize =
103 :     (double) target_rate / 8.0 / rate_control->framerate;
104 :     rate_control->sequence_quality = 2.0 / (double) rate_control->rtn_quant;
105 :     rate_control->avg_framesize = rate_control->target_framesize;
106 :    
107 :     rate_control->reaction_delay_factor = reaction_delay_factor;
108 :     rate_control->averaging_period = averaging_period;
109 :     rate_control->buffer = buffer;
110 :     }
111 :    
112 :     /*****************************************************************************
113 :     * RateControlGetQ
114 :     *
115 :     * Returned value : - the current 'rate_control' quantizer value
116 :     *
117 :     ****************************************************************************/
118 :    
119 :     int
120 :     RateControlGetQ(RateControl * rate_control,
121 :     int keyframe)
122 :     {
123 :     return rate_control->rtn_quant;
124 :     }
125 :    
126 :     /*****************************************************************************
127 :     * RateControlUpdate
128 :     *
129 :     * This function is called once a coded frame to update all internal
130 :     * parameters of 'rate_control'.
131 :     *
132 :     * Returned value : None
133 :     *
134 :     ****************************************************************************/
135 :    
136 :     void
137 :     RateControlUpdate(RateControl * rate_control,
138 :     int16_t quant,
139 :     int frame_size,
140 :     int keyframe)
141 :     {
142 :     int64_t deviation;
143 :     double overflow, averaging_period, reaction_delay_factor;
144 :     double quality_scale, base_quality, target_quality;
145 :     int32_t rtn_quant;
146 :    
147 :     rate_control->frames++;
148 :     rate_control->total_size += frame_size;
149 :    
150 :     deviation =
151 :     (int64_t) ((double) rate_control->total_size -
152 :     ((double)
153 :     ((double) rate_control->target_rate / 8.0 /
154 :     (double) rate_control->framerate) *
155 :     (double) rate_control->frames));
156 :    
157 : edgomez 514 DPRINTF(DPRINTF_DEBUG, "CBR: frame: %i, quant: %i, deviation: %i\n",
158 :     (int32_t) (rate_control->frames - 1), rate_control->rtn_quant,
159 :     (int32_t) deviation);
160 : edgomez 221
161 :     if (rate_control->rtn_quant >= 2) {
162 :     averaging_period = (double) rate_control->averaging_period;
163 :     rate_control->sequence_quality -=
164 :     rate_control->sequence_quality / averaging_period;
165 :     rate_control->sequence_quality +=
166 :     2.0 / (double) rate_control->rtn_quant / averaging_period;
167 :     if (rate_control->sequence_quality < 0.1)
168 :     rate_control->sequence_quality = 0.1;
169 :    
170 :     if (!keyframe) {
171 :     reaction_delay_factor =
172 :     (double) rate_control->reaction_delay_factor;
173 :     rate_control->avg_framesize -=
174 :     rate_control->avg_framesize / reaction_delay_factor;
175 :     rate_control->avg_framesize += frame_size / reaction_delay_factor;
176 :     }
177 :     }
178 :    
179 :     quality_scale =
180 :     rate_control->target_framesize / rate_control->avg_framesize *
181 :     rate_control->target_framesize / rate_control->avg_framesize;
182 :    
183 :     base_quality = rate_control->sequence_quality;
184 :     if (quality_scale >= 1.0) {
185 :     base_quality = 1.0 - (1.0 - base_quality) / quality_scale;
186 :     } else {
187 :     base_quality = 0.06452 + (base_quality - 0.06452) * quality_scale;
188 :     }
189 :    
190 :     overflow = -((double) deviation / (double) rate_control->buffer);
191 :    
192 :     target_quality =
193 :     base_quality + (base_quality -
194 :     0.06452) * overflow / rate_control->target_framesize;
195 :    
196 :     if (target_quality > 2.0)
197 :     target_quality = 2.0;
198 :     else if (target_quality < 0.06452)
199 :     target_quality = 0.06452;
200 :    
201 :     rtn_quant = (int32_t) (2.0 / target_quality);
202 :    
203 :     if (rtn_quant < 31) {
204 :     rate_control->quant_error[rtn_quant] +=
205 :     2.0 / target_quality - rtn_quant;
206 :     if (rate_control->quant_error[rtn_quant] >= 1.0) {
207 :     rate_control->quant_error[rtn_quant] -= 1.0;
208 :     rtn_quant++;
209 :     }
210 :     }
211 :    
212 :     if (rtn_quant > rate_control->rtn_quant + 1)
213 :     rtn_quant = rate_control->rtn_quant + 1;
214 :     else if (rtn_quant < rate_control->rtn_quant - 1)
215 :     rtn_quant = rate_control->rtn_quant - 1;
216 :    
217 :     if (rtn_quant > rate_control->max_quant)
218 :     rtn_quant = rate_control->max_quant;
219 :     else if (rtn_quant < rate_control->min_quant)
220 :     rtn_quant = rate_control->min_quant;
221 :    
222 :     rate_control->rtn_quant = rtn_quant;
223 :     }
224 :    
225 :     /*****************************************************************************
226 :     * Local functions
227 :     ****************************************************************************/
228 :    
229 :     static int
230 :     get_initial_quant(int bpp)
231 :     {
232 :     return 5;
233 :     }

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