29 |
#include "../xvid.h" |
#include "../xvid.h" |
30 |
#include "../portab.h" |
#include "../portab.h" |
31 |
|
|
32 |
|
/***************************************************************************** |
33 |
|
* Private data type |
34 |
|
****************************************************************************/ |
35 |
|
|
36 |
|
typedef struct |
37 |
|
{ |
38 |
|
float *quant; |
39 |
|
float *val; |
40 |
|
} lumi_data_t; |
41 |
|
|
42 |
/***************************************************************************** |
/***************************************************************************** |
43 |
* Prototypes |
* Sub plugin functions |
44 |
****************************************************************************/ |
****************************************************************************/ |
45 |
|
|
46 |
static int |
static int lumi_plg_info(xvid_plg_info_t *info); |
47 |
adaptive_quantization(unsigned char *buf, |
static int lumi_plg_create(xvid_plg_create_t *create, lumi_data_t **handle); |
48 |
int stride, |
static int lumi_plg_destroy(lumi_data_t *handle, xvid_plg_destroy_t * destroy); |
49 |
int *intquant, |
static int lumi_plg_before(lumi_data_t *handle, xvid_plg_data_t *data); |
50 |
int framequant, |
static int lumi_plg_after(lumi_data_t *handle, xvid_plg_data_t *data); |
|
int min_quant, |
|
|
int max_quant, |
|
|
int mb_width, |
|
|
int mb_height); |
|
|
|
|
51 |
|
|
52 |
/***************************************************************************** |
/***************************************************************************** |
53 |
* The plugin entry |
* The plugin entry function |
54 |
****************************************************************************/ |
****************************************************************************/ |
55 |
|
|
56 |
int |
int |
57 |
xvid_plugin_lumimasking(void * handle, int opt, void * param1, void * param2) |
xvid_plugin_lumimasking(void * handle, int opt, void * param1, void * param2) |
58 |
{ |
{ |
59 |
switch(opt) |
switch(opt) { |
|
{ |
|
60 |
case XVID_PLG_INFO : |
case XVID_PLG_INFO : |
61 |
|
return(lumi_plg_info((xvid_plg_info_t*)param1)); |
62 |
|
case XVID_PLG_CREATE: |
63 |
|
return(lumi_plg_create((xvid_plg_create_t *)param1, (lumi_data_t **)param2)); |
64 |
|
case XVID_PLG_DESTROY: |
65 |
|
return(lumi_plg_destroy((lumi_data_t *)handle, (xvid_plg_destroy_t*)param1)); |
66 |
|
case XVID_PLG_BEFORE : |
67 |
|
return(lumi_plg_before((lumi_data_t *)handle, (xvid_plg_data_t *)param1)); |
68 |
|
case XVID_PLG_AFTER : |
69 |
|
return(lumi_plg_after((lumi_data_t *)handle, (xvid_plg_data_t *)param1)); |
70 |
|
} |
71 |
|
|
72 |
|
return(XVID_ERR_FAIL); |
73 |
|
} |
74 |
|
|
75 |
|
/*---------------------------------------------------------------------------- |
76 |
|
* Info plugin function |
77 |
|
*--------------------------------------------------------------------------*/ |
78 |
|
|
79 |
|
static int |
80 |
|
lumi_plg_info(xvid_plg_info_t *info) |
81 |
{ |
{ |
82 |
xvid_plg_info_t * info = (xvid_plg_info_t*)param1; |
/* We just require a diff quant array access */ |
83 |
info->flags = XVID_REQDQUANTS; |
info->flags = XVID_REQDQUANTS; |
84 |
return 0; |
return(0); |
85 |
} |
} |
86 |
|
|
87 |
case XVID_PLG_CREATE : |
/*---------------------------------------------------------------------------- |
88 |
case XVID_PLG_DESTROY : |
* Create plugin function |
89 |
return 0; |
* |
90 |
|
* Allocates the private plugin data arrays |
91 |
|
*--------------------------------------------------------------------------*/ |
92 |
|
|
93 |
|
static int |
94 |
|
lumi_plg_create(xvid_plg_create_t *create, lumi_data_t **handle) |
95 |
|
{ |
96 |
|
lumi_data_t *lumi; |
97 |
|
|
98 |
case XVID_PLG_BEFORE : |
if ((lumi = (lumi_data_t*)malloc(sizeof(lumi_data_t))) == NULL) |
99 |
|
return(XVID_ERR_MEMORY); |
100 |
|
|
101 |
|
lumi->quant = (float*)malloc(create->mb_width*create->mb_height*sizeof(float)); |
102 |
|
if (lumi->quant == NULL) { |
103 |
|
free(lumi); |
104 |
|
return(XVID_ERR_MEMORY); |
105 |
|
} |
106 |
|
|
107 |
|
lumi->val = (float*)malloc(create->mb_width*create->mb_height*sizeof(float)); |
108 |
|
if (lumi->val == NULL) { |
109 |
|
free(lumi->quant); |
110 |
|
free(lumi); |
111 |
|
return(XVID_ERR_MEMORY); |
112 |
|
} |
113 |
|
|
114 |
|
/* Bind the data structure to the handle */ |
115 |
|
*handle = lumi; |
116 |
|
|
117 |
|
return(0); |
118 |
|
} |
119 |
|
|
120 |
|
/*---------------------------------------------------------------------------- |
121 |
|
* Destroy plugin function |
122 |
|
* |
123 |
|
* Free the private plugin data arrays |
124 |
|
*--------------------------------------------------------------------------*/ |
125 |
|
|
126 |
|
static int |
127 |
|
lumi_plg_destroy(lumi_data_t *handle, xvid_plg_destroy_t *destroy) |
128 |
{ |
{ |
129 |
xvid_plg_data_t * data = (xvid_plg_data_t*)param1; |
if (handle) { |
130 |
data->quant = |
if (handle->quant) { |
131 |
adaptive_quantization(data->current.plane[0], |
free(handle->quant); |
132 |
data->current.stride[0], |
handle->quant = NULL; |
133 |
data->dquant, |
} |
134 |
data->quant /* framequant*/, |
if (handle->val) { |
135 |
data->quant /* min_quant */, |
free(handle->val); |
136 |
data->quant*2 /* max_quant */, |
handle->val = NULL; |
137 |
data->mb_width, |
} |
138 |
data->mb_height); |
free(handle); |
139 |
|
} |
140 |
|
return(0); |
141 |
|
} |
142 |
|
|
143 |
|
/*---------------------------------------------------------------------------- |
144 |
|
* Before plugin function |
145 |
|
* |
146 |
|
* Here is all the magic about lumimasking. |
147 |
|
*--------------------------------------------------------------------------*/ |
148 |
|
|
149 |
|
/* Helper function defined later */ |
150 |
|
static int normalize_quantizer_field(float *in, |
151 |
|
int *out, |
152 |
|
int num, |
153 |
|
int min_quant, |
154 |
|
int max_quant); |
155 |
|
|
156 |
|
static int |
157 |
|
lumi_plg_before(lumi_data_t *handle, xvid_plg_data_t *data) |
158 |
|
{ |
159 |
|
int i, j; |
160 |
|
|
161 |
|
float global = 0.0f; |
162 |
|
uint32_t mid_range = 0; |
163 |
|
|
164 |
|
const float DarkAmpl = 14 / 2; |
165 |
|
const float BrightAmpl = 10 / 2; |
166 |
|
const float DarkThres = 70; |
167 |
|
const float BrightThres = 200; |
168 |
|
|
169 |
|
const float GlobalDarkThres = 60; |
170 |
|
const float GlobalBrightThres = 170; |
171 |
|
|
172 |
|
const float MidRangeThres = 20; |
173 |
|
const float UpperLimit = 200; |
174 |
|
const float LowerLimit = 25; |
175 |
|
|
176 |
|
/* Do this for all macroblocks individually */ |
177 |
|
for (j = 0; j < data->mb_height; j++) { |
178 |
|
for (i = 0; i < data->mb_width; i++) { |
179 |
|
int k, l; |
180 |
|
unsigned char *ptr; |
181 |
|
|
182 |
|
/* Initialize the current quant value to the frame quant */ |
183 |
|
handle->quant[j*data->mb_width + i] = (float)data->quant; |
184 |
|
|
185 |
return 0; |
/* Reset the val value */ |
186 |
|
handle->val[j*data->mb_width + i] = 0.0f; |
187 |
|
|
188 |
|
/* Next steps compute the luminance-masking */ |
189 |
|
|
190 |
|
/* Get the MB address */ |
191 |
|
ptr = data->current.plane[0]; |
192 |
|
ptr += 16*j*data->current.stride[0] + 16*i; |
193 |
|
|
194 |
|
/* Accumulate luminance */ |
195 |
|
for (k = 0; k < 16; k++) |
196 |
|
for (l = 0; l < 16; l++) |
197 |
|
handle->val[j*data->mb_width + i] += ptr[k*data->current.stride[0] + l]; |
198 |
|
|
199 |
|
/* Normalize its value */ |
200 |
|
handle->val[j*data->mb_width + i] /= 256.0f; |
201 |
|
|
202 |
|
/* Accumulate the global frame luminance */ |
203 |
|
global += handle->val[j*data->mb_width + i]; |
204 |
|
|
205 |
|
/* Count the number of middle luminance blocks */ |
206 |
|
if ((handle->val[j*data->mb_width + i] > LowerLimit) && |
207 |
|
(handle->val[j*data->mb_width + i] < UpperLimit)) |
208 |
|
mid_range++; |
209 |
|
} |
210 |
} |
} |
211 |
|
|
212 |
case XVID_PLG_AFTER : |
/* Normalize the global luminance accumulator */ |
213 |
return 0; |
global /= data->mb_width*data->mb_height; |
214 |
|
|
215 |
|
/* Apply luminance masking only to frames where the global luminance is |
216 |
|
* higher than DarkThreshold and lower than Bright Threshold, or where |
217 |
|
* "middle" luminance blocks are lower than its corresponding threshold */ |
218 |
|
if (((global < GlobalBrightThres) && (global > GlobalDarkThres)) || |
219 |
|
(mid_range < MidRangeThres)) { |
220 |
|
|
221 |
|
/* Apply the luminance masking formulas to all MBs */ |
222 |
|
for (i = 0; i < data->mb_height; i++) { |
223 |
|
for (j = 0; j < data->mb_width; j++) { |
224 |
|
if (handle->val[i*data->mb_width + j] < DarkThres) |
225 |
|
handle->quant[i*data->mb_width + j] += DarkAmpl * (DarkThres - handle->val[i*data->mb_width + j]) / DarkThres; |
226 |
|
else if (handle->val[i*data->mb_width + j] > BrightThres) |
227 |
|
handle->quant[i*data->mb_width + j] += BrightAmpl * (handle->val[i*data->mb_width + j] - BrightThres) / (255 - BrightThres); |
228 |
|
} |
229 |
|
} |
230 |
|
} |
231 |
|
|
232 |
|
/* Normalize the quantizer field */ |
233 |
|
data->quant = normalize_quantizer_field(handle->quant, |
234 |
|
data->dquant, |
235 |
|
data->mb_width*data->mb_height, |
236 |
|
data->quant, |
237 |
|
data->quant*2); |
238 |
|
|
239 |
|
/* Plugin job finished */ |
240 |
|
return(0); |
241 |
} |
} |
242 |
|
|
243 |
return XVID_ERR_FAIL; |
/*---------------------------------------------------------------------------- |
244 |
|
* After plugin function (dummy function) |
245 |
|
*--------------------------------------------------------------------------*/ |
246 |
|
|
247 |
|
static int |
248 |
|
lumi_plg_after(lumi_data_t *handle, xvid_plg_data_t *data) |
249 |
|
{ |
250 |
|
return(0); |
251 |
} |
} |
252 |
|
|
253 |
/***************************************************************************** |
/***************************************************************************** |
302 |
|
|
303 |
return (int) (in[0] + 0.5); |
return (int) (in[0] + 0.5); |
304 |
} |
} |
|
|
|
|
static int |
|
|
adaptive_quantization(unsigned char *buf, |
|
|
int stride, |
|
|
int *intquant, |
|
|
int framequant, |
|
|
int min_quant, |
|
|
int max_quant, |
|
|
int mb_width, |
|
|
int mb_height) /* no qstride because normalization */ |
|
|
{ |
|
|
int i, j, k, l; |
|
|
|
|
|
float *quant; |
|
|
unsigned char *ptr; |
|
|
float *val; |
|
|
float global = 0.; |
|
|
uint32_t mid_range = 0; |
|
|
|
|
|
const float DarkAmpl = 14 / 2; |
|
|
const float BrightAmpl = 10 / 2; |
|
|
const float DarkThres = 70; |
|
|
const float BrightThres = 200; |
|
|
|
|
|
const float GlobalDarkThres = 60; |
|
|
const float GlobalBrightThres = 170; |
|
|
|
|
|
const float MidRangeThres = 20; |
|
|
const float UpperLimit = 200; |
|
|
const float LowerLimit = 25; |
|
|
|
|
|
|
|
|
if (!(quant = (float *) malloc(mb_width * mb_height * sizeof(float)))) |
|
|
return(-1); |
|
|
|
|
|
if(!(val = (float *) malloc(mb_width * mb_height * sizeof(float)))) |
|
|
return(-1); |
|
|
|
|
|
for (k = 0; k < mb_height; k++) { |
|
|
for (l = 0; l < mb_width; l++) /* do this for all macroblocks individually */ |
|
|
{ |
|
|
quant[k * mb_width + l] = (float) framequant; |
|
|
|
|
|
/* calculate luminance-masking */ |
|
|
ptr = &buf[16 * k * stride + 16 * l]; /* address of MB */ |
|
|
|
|
|
val[k * mb_width + l] = 0.; |
|
|
|
|
|
for (i = 0; i < 16; i++) |
|
|
for (j = 0; j < 16; j++) |
|
|
val[k * mb_width + l] += ptr[i * stride + j]; |
|
|
val[k * mb_width + l] /= 256.; |
|
|
global +=val[k * mb_width + l]; |
|
|
|
|
|
if ((val[k * mb_width + l] > LowerLimit) && |
|
|
(val[k * mb_width + l] < UpperLimit)) |
|
|
mid_range++; |
|
|
} |
|
|
} |
|
|
|
|
|
global /=mb_width * mb_height; |
|
|
|
|
|
if (((global <GlobalBrightThres) &&(global >GlobalDarkThres)) |
|
|
|| (mid_range < MidRangeThres)) { |
|
|
for (k = 0; k < mb_height; k++) { |
|
|
for (l = 0; l < mb_width; l++) /* do this for all macroblocks individually */ |
|
|
{ |
|
|
if (val[k * mb_width + l] < DarkThres) |
|
|
quant[k * mb_width + l] += |
|
|
DarkAmpl * (DarkThres - |
|
|
val[k * mb_width + l]) / DarkThres; |
|
|
else if (val[k * mb_width + l] > BrightThres) |
|
|
quant[k * mb_width + l] += |
|
|
BrightAmpl * (val[k * mb_width + l] - |
|
|
BrightThres) / (255 - BrightThres); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
i = normalize_quantizer_field(quant, intquant, |
|
|
mb_width * mb_height, |
|
|
min_quant, max_quant); |
|
|
|
|
|
free(val); |
|
|
free(quant); |
|
|
|
|
|
return(i); |
|
|
|
|
|
} |
|