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

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

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