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

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

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