1 |
/***************************************************************************** |
/***************************************************************************** |
2 |
* |
* |
3 |
* XviD VBR Library |
* XviD Standard Plugins |
4 |
* - CBR/ABR bitrate controller implementation - |
* - CBR/ABR bitrate controller implementation - |
5 |
* |
* |
6 |
* Copyright (C) 2002 Edouard Gomez <ed.gomez@wanadoo.fr> |
* Copyright(C) 2002 Benjamin Lambert <foxer@hotmail.com> |
7 |
|
* 2002-2003 Edouard Gomez <ed.gomez@free.fr> |
8 |
* |
* |
9 |
* This program is free software; you can redistribute it and/or modify |
* This program is free software; you can redistribute it and/or modify |
10 |
* it under the terms of the GNU General Public License as published by |
* it under the terms of the GNU General Public License as published by |
20 |
* along with this program; if not, write to the Free Software |
* along with this program; if not, write to the Free Software |
21 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
22 |
* |
* |
23 |
* $Id: plugin_cbr.c,v 1.1.2.2 2003-03-23 09:39:29 suxen_drol Exp $ |
* $Id: plugin_cbr.c,v 1.1.2.3 2003-04-27 15:40:50 edgomez Exp $ |
24 |
* |
* |
25 |
****************************************************************************/ |
****************************************************************************/ |
26 |
|
|
27 |
|
|
28 |
|
#include <limits.h> |
29 |
|
|
30 |
#include "../xvid.h" |
#include "../xvid.h" |
31 |
#include "../image/image.h" |
#include "../image/image.h" |
32 |
|
|
33 |
#define INITIAL_QUANT 5 |
#define DEFAULT_INITIAL_QUANTIZER 5 |
34 |
|
|
35 |
|
#define DEFAULT_BITRATE 900000 /* 900kbps */ |
36 |
|
#define DEFAULT_MAX_QUANT 31 |
37 |
|
#define DEFAULT_MIN_QUANT 2 |
38 |
|
#define DEFAULT_DELAY_FACTOR 16 |
39 |
|
#define DEFAULT_AVERAGING_PERIOD 100 |
40 |
|
#define DEFAULT_BUFFER 100 |
41 |
|
|
42 |
typedef struct |
typedef struct |
43 |
{ |
{ |
58 |
double sequence_quality; |
double sequence_quality; |
59 |
double avg_framesize; |
double avg_framesize; |
60 |
double quant_error[31]; |
double quant_error[31]; |
61 |
} rc_cbr_t; |
} |
62 |
|
rc_cbr_t; |
63 |
|
|
64 |
|
|
65 |
|
static int |
66 |
|
get_initial_quant(unsigned int bitrate) |
67 |
|
{ |
68 |
|
|
69 |
|
#if 0 |
70 |
|
int i; |
71 |
|
|
72 |
|
const unsigned int bitrate_quant[31] = { |
73 |
|
UINT_MAX |
74 |
|
}; |
75 |
|
|
76 |
|
for (i = 30; i >= 0; i--) { |
77 |
|
if (bitrate > bitrate_quant[i]) |
78 |
|
continue; |
79 |
|
} |
80 |
|
|
81 |
|
return (i + 1); |
82 |
|
#else |
83 |
|
return (DEFAULT_INITIAL_QUANTIZER); |
84 |
|
#endif |
85 |
|
} |
86 |
|
|
87 |
static int rc_cbr_create(xvid_plg_create_t * create, rc_cbr_t ** handle) |
static int |
88 |
|
rc_cbr_create(xvid_plg_create_t * create, |
89 |
|
rc_cbr_t ** handle) |
90 |
{ |
{ |
91 |
xvid_plugin_cbr_t * param = (xvid_plugin_cbr_t *)create->param; |
xvid_plugin_cbr_t * param = (xvid_plugin_cbr_t *)create->param; |
92 |
rc_cbr_t * rc; |
rc_cbr_t * rc; |
93 |
int i; |
int i; |
94 |
|
|
95 |
/* cbr need to caclulate the average frame size. in order to do that, we need some fps */ |
/* |
96 |
|
* CBR needs to caclculate the average frame size. In order to do that, |
97 |
|
* we really need valid fps |
98 |
|
*/ |
99 |
if (create->fincr == 0) { |
if (create->fincr == 0) { |
100 |
return XVID_ERR_FAIL; |
return XVID_ERR_FAIL; |
101 |
} |
} |
102 |
|
|
103 |
/* allocate context struct */ |
/* Allocate context struct */ |
104 |
if((rc = malloc(sizeof(rc_cbr_t))) == NULL) |
if((rc = malloc(sizeof(rc_cbr_t))) == NULL) |
105 |
return(XVID_ERR_MEMORY); |
return(XVID_ERR_MEMORY); |
106 |
|
|
107 |
/* constants */ |
/* Constants */ |
108 |
rc->bytes_per_sec = param->bitrate / 8; |
rc->bytes_per_sec = (param->bitrate > 0) ? param->bitrate / 8 : DEFAULT_BITRATE / 8; |
109 |
rc->target_framesize = (double)rc->bytes_per_sec / ((double)create->fbase / create->fincr); |
rc->target_framesize = (double)rc->bytes_per_sec / ((double)create->fbase / create->fincr); |
110 |
rc->max_quantizer = param->max_quantizer>0 ? param->max_quantizer : 12; |
rc->max_quantizer = (param->max_quantizer > 0) ? param->max_quantizer : DEFAULT_MAX_QUANT; |
111 |
rc->min_quantizer = param->min_quantizer>0 ? param->min_quantizer : 2; |
rc->min_quantizer = (param->min_quantizer > 0) ? param->min_quantizer : DEFAULT_MIN_QUANT; |
112 |
rc->reaction_delay_factor = param->reaction_delay_factor>0 ? param->reaction_delay_factor : 16; |
rc->reaction_delay_factor = (param->reaction_delay_factor > 0) ? param->reaction_delay_factor : DEFAULT_DELAY_FACTOR; |
113 |
rc->averaging_period = param->averaging_period>0 ? param->averaging_period : 100; |
rc->averaging_period = (param->averaging_period > 0) ? param->averaging_period : DEFAULT_AVERAGING_PERIOD; |
114 |
rc->buffer = param->buffer>0 ? param->buffer : 100; |
rc->buffer = (param->buffer > 0) ? param->buffer : DEFAULT_BUFFER; |
115 |
|
|
116 |
rc->time = 0; |
rc->time = 0; |
117 |
rc->total_size = 0; |
rc->total_size = 0; |
118 |
rc->rtn_quant = INITIAL_QUANT; |
rc->rtn_quant = get_initial_quant(param->bitrate); |
119 |
|
|
120 |
/* Reset quant error accumulators */ |
/* Reset quant error accumulators */ |
121 |
for (i = 0; i < 31; i++) |
for (i = 0; i < 31; i++) |
122 |
rc->quant_error[i] = 0.0; |
rc->quant_error[i] = 0.0; |
123 |
|
|
124 |
/* Last bunch of variables */ |
/* Last bunch of variables */ |
125 |
|
rc->sequence_quality = 2.0 / (double) rc->rtn_quant; |
126 |
|
rc->avg_framesize = rc->target_framesize; |
127 |
|
|
128 |
|
/* Bind the RC */ |
129 |
|
*handle = rc; |
130 |
|
|
131 |
|
/* A bit of debug info */ |
132 |
DPRINTF(DPRINTF_RC, "bytes_per_sec: %i\n", rc->bytes_per_sec); |
DPRINTF(DPRINTF_RC, "bytes_per_sec: %i\n", rc->bytes_per_sec); |
133 |
DPRINTF(DPRINTF_RC, "frame rate : %f\n", (double)create->fbase / create->fincr); |
DPRINTF(DPRINTF_RC, "frame rate : %f\n", (double)create->fbase / create->fincr); |
134 |
DPRINTF(DPRINTF_RC, "target_framesize: %f\n",rc->target_framesize); |
DPRINTF(DPRINTF_RC, "target_framesize: %f\n",rc->target_framesize); |
135 |
|
|
|
rc->sequence_quality = 2.0 / (double) rc->rtn_quant; |
|
|
rc->avg_framesize = rc->target_framesize; |
|
|
|
|
|
*handle = rc; |
|
136 |
return(0); |
return(0); |
137 |
} |
} |
138 |
|
|
139 |
|
|
140 |
static int rc_cbr_destroy(rc_cbr_t * rc, xvid_plg_destroy_t * destroy) |
static int |
141 |
|
rc_cbr_destroy(rc_cbr_t * rc, |
142 |
|
xvid_plg_destroy_t * destroy) |
143 |
{ |
{ |
144 |
free(rc); |
free(rc); |
145 |
return(0); |
return(0); |
146 |
} |
} |
147 |
|
|
148 |
|
|
149 |
static int rc_cbr_before(rc_cbr_t * rc, xvid_plg_data_t * data) |
static int |
150 |
|
rc_cbr_before(rc_cbr_t * rc, |
151 |
|
xvid_plg_data_t * data) |
152 |
{ |
{ |
153 |
data->quant = rc->rtn_quant; |
data->quant = rc->rtn_quant; |
154 |
data->type = XVID_TYPE_AUTO; |
data->type = XVID_TYPE_AUTO; |
156 |
} |
} |
157 |
|
|
158 |
|
|
159 |
static int rc_cbr_after(rc_cbr_t * rc, xvid_plg_data_t * data) |
static int |
160 |
|
rc_cbr_after(rc_cbr_t * rc, |
161 |
|
xvid_plg_data_t * data) |
162 |
{ |
{ |
163 |
int64_t deviation; |
int64_t deviation; |
164 |
int rtn_quant; |
int rtn_quant; |
183 |
|
|
184 |
averaging_period = (double) rc->averaging_period; |
averaging_period = (double) rc->averaging_period; |
185 |
|
|
186 |
rc->sequence_quality -= |
rc->sequence_quality -= rc->sequence_quality / averaging_period; |
|
rc->sequence_quality / averaging_period; |
|
187 |
|
|
188 |
rc->sequence_quality += |
rc->sequence_quality += |
189 |
2.0 / (double) rc->rtn_quant / averaging_period; |
2.0 / (double) rc->rtn_quant / averaging_period; |
200 |
} |
} |
201 |
|
|
202 |
quality_scale = |
quality_scale = |
203 |
rc->target_framesize / rc->avg_framesize * |
rc->target_framesize / rc->avg_framesize * rc->target_framesize / |
204 |
rc->target_framesize / rc->avg_framesize; |
rc->avg_framesize; |
205 |
|
|
206 |
base_quality = rc->sequence_quality; |
base_quality = rc->sequence_quality; |
207 |
if (quality_scale >= 1.0) { |
if (quality_scale >= 1.0) { |
213 |
overflow = -((double) deviation / (double) rc->buffer); |
overflow = -((double) deviation / (double) rc->buffer); |
214 |
|
|
215 |
target_quality = |
target_quality = |
216 |
base_quality + |
base_quality + (base_quality - |
217 |
(base_quality - 0.06452) * overflow / rc->target_framesize; |
0.06452) * overflow / rc->target_framesize; |
218 |
|
|
219 |
if (target_quality > 2.0) |
if (target_quality > 2.0) |
220 |
target_quality = 2.0; |
target_quality = 2.0; |
223 |
|
|
224 |
rtn_quant = (int) (2.0 / target_quality); |
rtn_quant = (int) (2.0 / target_quality); |
225 |
|
|
226 |
if (rtn_quant < 31) { |
if (rtn_quant > 0 && rtn_quant < 31) { |
227 |
rc->quant_error[rtn_quant-1] += |
rc->quant_error[rtn_quant - 1] += 2.0 / target_quality - rtn_quant; |
|
2.0 / target_quality - rtn_quant; |
|
228 |
if (rc->quant_error[rtn_quant-1] >= 1.0) { |
if (rc->quant_error[rtn_quant-1] >= 1.0) { |
229 |
rc->quant_error[rtn_quant-1] -= 1.0; |
rc->quant_error[rtn_quant-1] -= 1.0; |
230 |
rtn_quant++; |
rtn_quant++; |
248 |
|
|
249 |
|
|
250 |
|
|
251 |
int xvid_plugin_cbr(void * handle, int opt, void * param1, void * param2) |
int |
252 |
{ |
xvid_plugin_cbr(void *handle, |
253 |
switch(opt) |
int opt, |
254 |
|
void *param1, |
255 |
|
void *param2) |
256 |
{ |
{ |
257 |
|
switch (opt) { |
258 |
case XVID_PLG_INFO : |
case XVID_PLG_INFO : |
259 |
return 0; |
return 0; |
260 |
|
|
273 |
|
|
274 |
return XVID_ERR_FAIL; |
return XVID_ERR_FAIL; |
275 |
} |
} |
|
|
|