1 |
|
/***************************************************************************** |
2 |
|
* |
3 |
|
* XVID MPEG-4 VIDEO CODEC |
4 |
|
* - XviD plugin: performs a lumimasking algorithm on encoded frame - |
5 |
|
* |
6 |
|
* Copyright(C) 2002-2003 Peter Ross <pross@xvid.org> |
7 |
|
* 2002 Christoph Lampert <gruel@web.de> |
8 |
|
* 2008 Jason Garrett-Glaser <darkshikari@gmail.com> |
9 |
|
* |
10 |
|
* This program is free software ; you can redistribute it and/or modify |
11 |
|
* it under the terms of the GNU General Public License as published by |
12 |
|
* the Free Software Foundation ; either version 2 of the License, or |
13 |
|
* (at your option) any later version. |
14 |
|
* |
15 |
|
* This program is distributed in the hope that it will be useful, |
16 |
|
* but WITHOUT ANY WARRANTY ; without even the implied warranty of |
17 |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 |
|
* GNU General Public License for more details. |
19 |
|
* |
20 |
|
* You should have received a copy of the GNU General Public License |
21 |
|
* along with this program ; if not, write to the Free Software |
22 |
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
23 |
|
* |
24 |
|
* $Id$ |
25 |
|
* |
26 |
|
****************************************************************************/ |
27 |
|
|
28 |
|
#include <stdlib.h> |
29 |
|
#include <stdio.h> |
30 |
|
#include <math.h> |
31 |
|
|
32 |
#include "../xvid.h" |
#include "../xvid.h" |
33 |
#include "../image/image.h" |
#include "../global.h" |
34 |
#include "../quant/adapt_quant.h" |
#include "../portab.h" |
35 |
|
#include "../utils/emms.h" |
36 |
|
|
37 |
|
/***************************************************************************** |
38 |
|
* Private data type |
39 |
|
****************************************************************************/ |
40 |
|
|
41 |
int xvid_plugin_lumimasking(void * handle, int opt, void * param1, void * param2) |
typedef struct |
42 |
{ |
{ |
43 |
switch(opt) |
float *quant; |
44 |
|
float *val; |
45 |
|
int method; |
46 |
|
} lumi_data_t; |
47 |
|
|
48 |
|
/***************************************************************************** |
49 |
|
* Sub plugin functions |
50 |
|
****************************************************************************/ |
51 |
|
|
52 |
|
static int lumi_plg_info(xvid_plg_info_t *info); |
53 |
|
static int lumi_plg_create(xvid_plg_create_t *create, lumi_data_t **handle); |
54 |
|
static int lumi_plg_destroy(lumi_data_t *handle, xvid_plg_destroy_t * destroy); |
55 |
|
static int lumi_plg_frame(lumi_data_t *handle, xvid_plg_data_t *data); |
56 |
|
static int lumi_plg_after(lumi_data_t *handle, xvid_plg_data_t *data); |
57 |
|
|
58 |
|
/***************************************************************************** |
59 |
|
* The plugin entry function |
60 |
|
****************************************************************************/ |
61 |
|
|
62 |
|
int |
63 |
|
xvid_plugin_lumimasking(void * handle, int opt, void * param1, void * param2) |
64 |
{ |
{ |
65 |
|
switch(opt) { |
66 |
case XVID_PLG_INFO : |
case XVID_PLG_INFO : |
67 |
|
return(lumi_plg_info((xvid_plg_info_t*)param1)); |
68 |
|
case XVID_PLG_CREATE: |
69 |
|
return(lumi_plg_create((xvid_plg_create_t *)param1, (lumi_data_t **)param2)); |
70 |
|
case XVID_PLG_DESTROY: |
71 |
|
return(lumi_plg_destroy((lumi_data_t *)handle, (xvid_plg_destroy_t*)param1)); |
72 |
|
case XVID_PLG_BEFORE : |
73 |
|
return 0; |
74 |
|
case XVID_PLG_FRAME : |
75 |
|
return(lumi_plg_frame((lumi_data_t *)handle, (xvid_plg_data_t *)param1)); |
76 |
|
case XVID_PLG_AFTER : |
77 |
|
return(lumi_plg_after((lumi_data_t *)handle, (xvid_plg_data_t *)param1)); |
78 |
|
} |
79 |
|
|
80 |
|
return(XVID_ERR_FAIL); |
81 |
|
} |
82 |
|
|
83 |
|
/*---------------------------------------------------------------------------- |
84 |
|
* Info plugin function |
85 |
|
*--------------------------------------------------------------------------*/ |
86 |
|
|
87 |
|
static int |
88 |
|
lumi_plg_info(xvid_plg_info_t *info) |
89 |
{ |
{ |
90 |
xvid_plg_info_t * info = (xvid_plg_info_t*)param1; |
/* We just require a diff quant array access */ |
91 |
info->flags = XVID_REQDQUANTS; |
info->flags = XVID_REQDQUANTS; |
92 |
return 0; |
return(0); |
93 |
} |
} |
94 |
|
|
95 |
case XVID_PLG_CREATE : |
/*---------------------------------------------------------------------------- |
96 |
case XVID_PLG_DESTROY : |
* Create plugin function |
97 |
return 0; |
* |
98 |
|
* Allocates the private plugin data arrays |
99 |
|
*--------------------------------------------------------------------------*/ |
100 |
|
|
101 |
|
static int |
102 |
|
lumi_plg_create(xvid_plg_create_t *create, lumi_data_t **handle) |
103 |
|
{ |
104 |
|
lumi_data_t *lumi; |
105 |
|
xvid_plugin_lumimasking_t *param = (xvid_plugin_lumimasking_t *) create->param; |
106 |
|
|
107 |
case XVID_PLG_BEFORE : |
if ((lumi = (lumi_data_t*)malloc(sizeof(lumi_data_t))) == NULL) |
108 |
|
return(XVID_ERR_MEMORY); |
109 |
|
|
110 |
|
lumi->method = 0; |
111 |
|
lumi->quant = (float*)malloc(create->mb_width*create->mb_height*sizeof(float)); |
112 |
|
if (lumi->quant == NULL) { |
113 |
|
free(lumi); |
114 |
|
return(XVID_ERR_MEMORY); |
115 |
|
} |
116 |
|
|
117 |
|
lumi->val = (float*)malloc(create->mb_width*create->mb_height*sizeof(float)); |
118 |
|
if (lumi->val == NULL) { |
119 |
|
free(lumi->quant); |
120 |
|
free(lumi); |
121 |
|
return(XVID_ERR_MEMORY); |
122 |
|
} |
123 |
|
|
124 |
|
if (param != NULL) |
125 |
|
lumi->method = param->method; |
126 |
|
|
127 |
|
/* Bind the data structure to the handle */ |
128 |
|
*handle = lumi; |
129 |
|
|
130 |
|
return(0); |
131 |
|
} |
132 |
|
|
133 |
|
/*---------------------------------------------------------------------------- |
134 |
|
* Destroy plugin function |
135 |
|
* |
136 |
|
* Free the private plugin data arrays |
137 |
|
*--------------------------------------------------------------------------*/ |
138 |
|
|
139 |
|
static int |
140 |
|
lumi_plg_destroy(lumi_data_t *handle, xvid_plg_destroy_t *destroy) |
141 |
{ |
{ |
142 |
xvid_plg_data_t * data = (xvid_plg_data_t*)param1; |
if (handle) { |
143 |
data->quant = |
if (handle->quant) { |
144 |
adaptive_quantization(data->current.plane[0], data->current.stride[0], |
free(handle->quant); |
145 |
data->dquant, |
handle->quant = NULL; |
146 |
data->quant /* framequant*/, |
} |
147 |
data->quant /* min_quant */, |
if (handle->val) { |
148 |
data->quant*2 /* max_quant */, |
free(handle->val); |
149 |
data->mb_width, data->mb_height); |
handle->val = NULL; |
150 |
|
} |
151 |
|
free(handle); |
152 |
|
} |
153 |
|
return(0); |
154 |
|
} |
155 |
|
|
156 |
return 0; |
/*---------------------------------------------------------------------------- |
157 |
|
* Before plugin function |
158 |
|
* |
159 |
|
* Here is all the magic about lumimasking. |
160 |
|
*--------------------------------------------------------------------------*/ |
161 |
|
|
162 |
|
/* Helper function defined later */ |
163 |
|
static int normalize_quantizer_field(float *in, |
164 |
|
int *out, |
165 |
|
int num, |
166 |
|
int min_quant, |
167 |
|
int max_quant); |
168 |
|
|
169 |
|
static int |
170 |
|
lumi_plg_frame(lumi_data_t *handle, xvid_plg_data_t *data) |
171 |
|
{ |
172 |
|
int i, j; |
173 |
|
|
174 |
|
float global = 0.0f; |
175 |
|
|
176 |
|
const float DarkAmpl = 14 / 4; |
177 |
|
const float BrightAmpl = 10 / 3; |
178 |
|
float DarkThres = 90; |
179 |
|
float BrightThres = 200; |
180 |
|
|
181 |
|
const float GlobalDarkThres = 60; |
182 |
|
const float GlobalBrightThres = 170; |
183 |
|
|
184 |
|
/* Arbitrary centerpoint for variance-based AQ. Roughly the same as used in x264. */ |
185 |
|
float center = 14000.f; |
186 |
|
/* Arbitrary strength for variance-based AQ. */ |
187 |
|
float strength = 0.2f; |
188 |
|
|
189 |
|
if (data->type == XVID_TYPE_BVOP) return 0; |
190 |
|
|
191 |
|
/* Do this for all macroblocks individually */ |
192 |
|
for (j = 0; j < data->mb_height; j++) { |
193 |
|
for (i = 0; i < data->mb_width; i++) { |
194 |
|
int k, l, sum = 0, sum_of_squares = 0; |
195 |
|
unsigned char *ptr; |
196 |
|
|
197 |
|
/* Initialize the current quant value to the frame quant */ |
198 |
|
handle->quant[j*data->mb_width + i] = (float)data->quant; |
199 |
|
|
200 |
|
/* Next steps compute the luminance-masking */ |
201 |
|
|
202 |
|
/* Get the MB address */ |
203 |
|
ptr = data->current.plane[0]; |
204 |
|
ptr += 16*j*data->current.stride[0] + 16*i; |
205 |
|
|
206 |
|
if (handle->method) { /* Variance masking mode */ |
207 |
|
/* Accumulate sum and sum of squares over the MB */ |
208 |
|
for (k = 0; k < 16; k++) { |
209 |
|
for (l = 0; l < 16; l++) { |
210 |
|
int val = ptr[k*data->current.stride[0] + l]; |
211 |
|
sum += val; |
212 |
|
sum_of_squares += val * val; |
213 |
|
} |
214 |
|
} |
215 |
|
/* Variance = SSD - SAD^2 / (numpixels) */ |
216 |
|
int variance = sum_of_squares - sum * sum / 256; |
217 |
|
handle->val[j*data->mb_width + i] = (float)variance; |
218 |
} |
} |
219 |
|
else { /* Luminance masking mode */ |
220 |
|
/* Accumulate luminance */ |
221 |
|
for (k = 0; k < 16; k++) |
222 |
|
for (l = 0; l < 16; l++) |
223 |
|
sum += ptr[k*data->current.stride[0] + l]; |
224 |
|
|
225 |
case XVID_PLG_AFTER : |
handle->val[j*data->mb_width + i] = (float)sum/256.0f; |
226 |
return 0; |
|
227 |
|
/* Accumulate the global frame luminance */ |
228 |
|
global += (float)sum/256.0f; |
229 |
|
} |
230 |
|
} |
231 |
} |
} |
232 |
|
|
233 |
return XVID_ERR_FAIL; |
if (handle->method) { /* Variance masking */ |
234 |
|
/* Apply the variance masking formula to all MBs */ |
235 |
|
for (i = 0; i < data->mb_height; i++) |
236 |
|
{ |
237 |
|
for (j = 0; j < data->mb_width; j++) |
238 |
|
{ |
239 |
|
float value = handle->val[i*data->mb_width + j]; |
240 |
|
float qscale_diff = strength * logf(value / center); |
241 |
|
handle->quant[i*data->mb_width + j] *= (1.0f + qscale_diff); |
242 |
|
} |
243 |
|
} |
244 |
|
} |
245 |
|
else { /* Luminance masking */ |
246 |
|
/* Normalize the global luminance accumulator */ |
247 |
|
global /= data->mb_width*data->mb_height; |
248 |
|
|
249 |
|
DarkThres = DarkThres*global/127.0f; |
250 |
|
BrightThres = BrightThres*global/127.0f; |
251 |
|
|
252 |
|
|
253 |
|
/* Apply luminance masking only to frames where the global luminance is |
254 |
|
* higher than DarkThreshold and lower than Bright Threshold */ |
255 |
|
if ((global < GlobalBrightThres) && (global > GlobalDarkThres)) { |
256 |
|
|
257 |
|
/* Apply the luminance masking formulas to all MBs */ |
258 |
|
for (i = 0; i < data->mb_height; i++) { |
259 |
|
for (j = 0; j < data->mb_width; j++) { |
260 |
|
if (handle->val[i*data->mb_width + j] < DarkThres) |
261 |
|
handle->quant[i*data->mb_width + j] *= 1 + DarkAmpl * (DarkThres - handle->val[i*data->mb_width + j]) / DarkThres; |
262 |
|
else if (handle->val[i*data->mb_width + j] > BrightThres) |
263 |
|
handle->quant[i*data->mb_width + j] *= 1 + BrightAmpl * (handle->val[i*data->mb_width + j] - BrightThres) / (255 - BrightThres); |
264 |
|
} |
265 |
|
} |
266 |
|
} |
267 |
} |
} |
268 |
|
|
269 |
|
/* Normalize the quantizer field */ |
270 |
|
data->quant = normalize_quantizer_field(handle->quant, |
271 |
|
data->dquant, |
272 |
|
data->mb_width*data->mb_height, |
273 |
|
data->quant, |
274 |
|
MAX(2,data->quant + data->quant/2)); |
275 |
|
|
276 |
|
/* Plugin job finished */ |
277 |
|
return(0); |
278 |
|
} |
279 |
|
|
280 |
|
/*---------------------------------------------------------------------------- |
281 |
|
* After plugin function (dummy function) |
282 |
|
*--------------------------------------------------------------------------*/ |
283 |
|
|
284 |
|
static int |
285 |
|
lumi_plg_after(lumi_data_t *handle, xvid_plg_data_t *data) |
286 |
|
{ |
287 |
|
return(0); |
288 |
|
} |
289 |
|
|
290 |
|
/***************************************************************************** |
291 |
|
* Helper functions |
292 |
|
****************************************************************************/ |
293 |
|
|
294 |
|
#define RDIFF(a, b) ((int)(a+0.5)-(int)(b+0.5)) |
295 |
|
|
296 |
|
static int |
297 |
|
normalize_quantizer_field(float *in, |
298 |
|
int *out, |
299 |
|
int num, |
300 |
|
int min_quant, |
301 |
|
int max_quant) |
302 |
|
{ |
303 |
|
int i; |
304 |
|
int finished; |
305 |
|
|
306 |
|
do { |
307 |
|
finished = 1; |
308 |
|
for (i = 1; i < num; i++) { |
309 |
|
if (RDIFF(in[i], in[i - 1]) > 2) { |
310 |
|
in[i] -= (float) 0.5; |
311 |
|
finished = 0; |
312 |
|
} else if (RDIFF(in[i], in[i - 1]) < -2) { |
313 |
|
in[i - 1] -= (float) 0.5; |
314 |
|
finished = 0; |
315 |
|
} |
316 |
|
|
317 |
|
if (in[i] > max_quant) { |
318 |
|
in[i] = (float) max_quant; |
319 |
|
finished = 0; |
320 |
|
} |
321 |
|
if (in[i] < min_quant) { |
322 |
|
in[i] = (float) min_quant; |
323 |
|
finished = 0; |
324 |
|
} |
325 |
|
if (in[i - 1] > max_quant) { |
326 |
|
in[i - 1] = (float) max_quant; |
327 |
|
finished = 0; |
328 |
|
} |
329 |
|
if (in[i - 1] < min_quant) { |
330 |
|
in[i - 1] = (float) min_quant; |
331 |
|
finished = 0; |
332 |
|
} |
333 |
|
} |
334 |
|
} while (!finished); |
335 |
|
|
336 |
|
out[0] = 0; |
337 |
|
for (i = 1; i < num; i++) |
338 |
|
out[i] = RDIFF(in[i], in[i - 1]); |
339 |
|
|
340 |
|
return (int) (in[0] + 0.5); |
341 |
|
} |