Parent Directory | Revision Log
Revision 542 - (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 : | * 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 : | h | 542 | * $Id: ratecontrol.c,v 1.17 2002-09-26 00:33:00 h 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 : | edgomez | 514 | DPRINTF(DPRINTF_DEBUG, "CBR: frame: %i, quant: %i, deviation: %i\n", |
136 : | (int32_t) (rate_control->frames - 1), rate_control->rtn_quant, | ||
137 : | (int32_t) deviation); | ||
138 : | edgomez | 221 | |
139 : | if (rate_control->rtn_quant >= 2) { | ||
140 : | averaging_period = (double) rate_control->averaging_period; | ||
141 : | rate_control->sequence_quality -= | ||
142 : | rate_control->sequence_quality / averaging_period; | ||
143 : | rate_control->sequence_quality += | ||
144 : | 2.0 / (double) rate_control->rtn_quant / averaging_period; | ||
145 : | if (rate_control->sequence_quality < 0.1) | ||
146 : | rate_control->sequence_quality = 0.1; | ||
147 : | |||
148 : | if (!keyframe) { | ||
149 : | reaction_delay_factor = | ||
150 : | (double) rate_control->reaction_delay_factor; | ||
151 : | rate_control->avg_framesize -= | ||
152 : | rate_control->avg_framesize / reaction_delay_factor; | ||
153 : | rate_control->avg_framesize += frame_size / reaction_delay_factor; | ||
154 : | } | ||
155 : | } | ||
156 : | |||
157 : | quality_scale = | ||
158 : | rate_control->target_framesize / rate_control->avg_framesize * | ||
159 : | rate_control->target_framesize / rate_control->avg_framesize; | ||
160 : | |||
161 : | base_quality = rate_control->sequence_quality; | ||
162 : | if (quality_scale >= 1.0) { | ||
163 : | base_quality = 1.0 - (1.0 - base_quality) / quality_scale; | ||
164 : | } else { | ||
165 : | base_quality = 0.06452 + (base_quality - 0.06452) * quality_scale; | ||
166 : | } | ||
167 : | |||
168 : | overflow = -((double) deviation / (double) rate_control->buffer); | ||
169 : | |||
170 : | target_quality = | ||
171 : | base_quality + (base_quality - | ||
172 : | 0.06452) * overflow / rate_control->target_framesize; | ||
173 : | |||
174 : | if (target_quality > 2.0) | ||
175 : | target_quality = 2.0; | ||
176 : | else if (target_quality < 0.06452) | ||
177 : | target_quality = 0.06452; | ||
178 : | |||
179 : | rtn_quant = (int32_t) (2.0 / target_quality); | ||
180 : | |||
181 : | if (rtn_quant < 31) { | ||
182 : | rate_control->quant_error[rtn_quant] += | ||
183 : | 2.0 / target_quality - rtn_quant; | ||
184 : | if (rate_control->quant_error[rtn_quant] >= 1.0) { | ||
185 : | rate_control->quant_error[rtn_quant] -= 1.0; | ||
186 : | rtn_quant++; | ||
187 : | } | ||
188 : | } | ||
189 : | |||
190 : | if (rtn_quant > rate_control->rtn_quant + 1) | ||
191 : | rtn_quant = rate_control->rtn_quant + 1; | ||
192 : | else if (rtn_quant < rate_control->rtn_quant - 1) | ||
193 : | rtn_quant = rate_control->rtn_quant - 1; | ||
194 : | |||
195 : | if (rtn_quant > rate_control->max_quant) | ||
196 : | rtn_quant = rate_control->max_quant; | ||
197 : | else if (rtn_quant < rate_control->min_quant) | ||
198 : | rtn_quant = rate_control->min_quant; | ||
199 : | |||
200 : | rate_control->rtn_quant = rtn_quant; | ||
201 : | } | ||
202 : | |||
203 : | /***************************************************************************** | ||
204 : | * Local functions | ||
205 : | ****************************************************************************/ | ||
206 : | |||
207 : | static int | ||
208 : | get_initial_quant(int bpp) | ||
209 : | { | ||
210 : | return 5; | ||
211 : | } |
No admin address has been configured | ViewVC Help |
Powered by ViewVC 1.0.4 |