00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include "stdio.h"
00039
00040 extern FILE *ftrace;
00041 extern int max_quantizer, min_quantizer;
00042
00043 typedef struct _rc_param_ {
00044 double quant;
00045 int rc_period;
00046 double target_rate;
00047 double average_rate;
00048 double reaction_rate;
00049 double average_delta;
00050 double reaction_delta;
00051 double reaction_ratio;
00052 } RC_Param;
00053
00054 static RC_Param rc_param;
00055
00056 void RateCtlInit(double quant, double target_rate,
00057 long rc_period, long rc_reaction_period, long rc_reaction_ratio)
00058 {
00059 #ifdef _RC_
00060 fprintf(ftrace, "Initializing Rate Control module:\n");
00061 fprintf(ftrace, "Initial quantizer is %f.\n", quant);
00062 fprintf(ftrace, "Target rate is %f bits per frame.\n", target_rate);
00063 fprintf(ftrace, "RC averaging period is %d.\n", rc_period);
00064 fprintf(ftrace, "RC reaction period is %d.\n", rc_reaction_period);
00065 fprintf(ftrace, "RC reaction ratio is %d.\n", rc_reaction_ratio);
00066 #endif
00067
00068 rc_param.quant = quant;
00069 rc_param.rc_period = rc_period;
00070 rc_param.target_rate = target_rate;
00071 rc_param.reaction_ratio = rc_reaction_ratio;
00072
00073 rc_param.average_delta = 1. / rc_period;
00074 rc_param.reaction_delta = 1. / rc_reaction_period;
00075 rc_param.average_rate = target_rate;
00076 rc_param.reaction_rate = target_rate;
00077
00078 return;
00079 }
00080
00081 int RateCtlGetQ(double MAD)
00082 {
00083 double quant;
00084
00085 quant = rc_param.quant;
00086
00087
00088
00089
00090 return (int)(quant + 0.5);
00091 }
00092
00093 void RateCtlUpdate(int current_frame)
00094 {
00095 double rate, delta, decay;
00096 double target, current_target;
00097 double median_quant;
00098
00099 #ifdef _RC_
00100 fprintf(ftrace, "Quantizer is currently %f.\n", rc_param.quant);
00101 fprintf(ftrace, "Current frame is %d bits long.\n", current_frame);
00102 #endif
00103
00104 rate = rc_param.average_rate;
00105 delta = rc_param.average_delta;
00106 decay = 1 - delta;
00107 rate = rate * decay + current_frame * delta;
00108 rc_param.average_rate = rate;
00109
00110 target = rc_param.target_rate;
00111 if (rate > target) {
00112 current_target = target - (rate - target);
00113 if (current_target < target * 0.75) current_target = target * 0.75;
00114 } else {
00115 current_target = target;
00116 }
00117
00118 #ifdef _RC_
00119 fprintf(ftrace, "Target rate is %f.\n", target);
00120 fprintf(ftrace, "Average rate is %f.\n", rate);
00121 fprintf(ftrace, "Target rate for current frame is %f.\n", current_target);
00122 #endif
00123
00124 rate = rc_param.reaction_rate;
00125 delta = rc_param.reaction_delta;
00126 decay = 1 - delta;
00127 rate = rate * decay + current_frame * delta;
00128 rc_param.reaction_rate = rate;
00129
00130 median_quant = min_quantizer + (max_quantizer - min_quantizer) / 2;
00131
00132
00133 if (rate < current_target) rc_param.quant *=
00134 (1 - rc_param.reaction_delta * ((current_target - rate) / current_target / 0.20) );
00135 if (rc_param.quant < min_quantizer) rc_param.quant = min_quantizer;
00136
00137
00138 if (rate > current_target) {
00139
00140 if (rc_param.quant > median_quant)
00141 rc_param.quant *= (1 + rc_param.reaction_delta / rc_param.reaction_ratio);
00142
00143 else if (rate > current_target * 1.20) rc_param.quant *=
00144 (1 + rc_param.reaction_delta);
00145 else rc_param.quant *=
00146 (1 + rc_param.reaction_delta * ((rate - current_target) / current_target / 0.20) );
00147 }
00148 if (rc_param.quant > max_quantizer) rc_param.quant = max_quantizer;
00149
00150 #ifdef _RC_
00151 fprintf(ftrace, "Reaction rate is %f.\n", rate);
00152 fprintf(ftrace, "Quantizer is updated to %f.\n", rc_param.quant);
00153 #endif
00154
00155 return;
00156 }
00157