[svn] / branches / dev-api-4 / xvidcore / src / utils / ratecontrol.c Repository:
ViewVC logotype

Annotation of /branches/dev-api-4/xvidcore/src/utils/ratecontrol.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1054 - (view) (download)

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

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