diff -urp xvidcore.orig/src/plugins/plugin_lumimasking.c xvidcore/src/plugins/plugin_lumimasking.c --- xvidcore.orig/src/plugins/plugin_lumimasking.c 2006-05-06 06:37:15 +0200 +++ xvidcore/src/plugins/plugin_lumimasking.c 2010-04-08 03:08:30 +0200 @@ -25,6 +25,7 @@ ****************************************************************************/ #include +#include #include "../xvid.h" #include "../global.h" @@ -160,24 +161,25 @@ static int normalize_quantizer_field(flo static int lumi_plg_frame(lumi_data_t *handle, xvid_plg_data_t *data) { - int i, j; - - float global = 0.0f; + //Don't apply variance-masking to B-frames. + if (data->type == XVID_TYPE_BVOP) return 0; - const float DarkAmpl = 14 / 4; - const float BrightAmpl = 10 / 3; - float DarkThres = 90; - float BrightThres = 200; + int i, j; - const float GlobalDarkThres = 60; - const float GlobalBrightThres = 170; + /* Arbitrary centerpoint for variance-based AQ. Roughly the same as used in x264. */ + float center = 14000; - if (data->type == XVID_TYPE_BVOP) return 0; + /* Arbitrary strength for variance-based AQ. */ + float strength = 0.2; /* Do this for all macroblocks individually */ - for (j = 0; j < data->mb_height; j++) { - for (i = 0; i < data->mb_width; i++) { - int k, l, sum = 0; + for (j = 0; j < data->mb_height; j++) + { + for (i = 0; i < data->mb_width; i++) + { + int k, l; + unsigned int sum = 0; + unsigned int sum_of_squares = 0; unsigned char *ptr; /* Initialize the current quant value to the frame quant */ @@ -189,37 +191,30 @@ lumi_plg_frame(lumi_data_t *handle, xvid ptr = data->current.plane[0]; ptr += 16*j*data->current.stride[0] + 16*i; - /* Accumulate luminance */ + /* Accumulate sum and sum of squares over the MB */ for (k = 0; k < 16; k++) for (l = 0; l < 16; l++) - sum += ptr[k*data->current.stride[0] + l]; + { + int val = ptr[k*data->current.stride[0] + l]; + sum += val; + sum_of_squares += val * val; + } + + /* Variance = SSD - SAD^2 / (numpixels) */ + int variance = sum_of_squares - sum * sum / 256; - handle->val[j*data->mb_width + i] = (float)sum/256.0f; - - /* Accumulate the global frame luminance */ - global += (float)sum/256.0f; + handle->val[j*data->mb_width + i] = variance; } } - - /* Normalize the global luminance accumulator */ - global /= data->mb_width*data->mb_height; - - DarkThres = DarkThres*global/127.0f; - BrightThres = BrightThres*global/127.0f; - - - /* Apply luminance masking only to frames where the global luminance is - * higher than DarkThreshold and lower than Bright Threshold */ - if ((global < GlobalBrightThres) && (global > GlobalDarkThres)) { - - /* Apply the luminance masking formulas to all MBs */ - for (i = 0; i < data->mb_height; i++) { - for (j = 0; j < data->mb_width; j++) { - if (handle->val[i*data->mb_width + j] < DarkThres) - handle->quant[i*data->mb_width + j] *= 1 + DarkAmpl * (DarkThres - handle->val[i*data->mb_width + j]) / DarkThres; - else if (handle->val[i*data->mb_width + j] > BrightThres) - handle->quant[i*data->mb_width + j] *= 1 + BrightAmpl * (handle->val[i*data->mb_width + j] - BrightThres) / (255 - BrightThres); - } + + /* Apply the variance masking formula to all MBs */ + for (i = 0; i < data->mb_height; i++) + { + for (j = 0; j < data->mb_width; j++) + { + float value = handle->val[i*data->mb_width + j]; + float qscale_diff = strength * logf(value / center); + handle->quant[i*data->mb_width + j] *= (1.0 + qscale_diff); } } diff -urp xvidcore.orig/src/xvid.c xvidcore/src/xvid.c --- xvidcore.orig/src/xvid.c 2009-05-25 10:09:23 +0200 +++ xvidcore/src/xvid.c 2010-04-08 03:09:08 +0200 @@ -665,7 +665,7 @@ xvid_gbl_info(xvid_gbl_info_t * info) return XVID_ERR_VERSION; info->actual_version = XVID_VERSION; - info->build = "xvid-1.2.2"; + info->build = "xvid-1.2.2-vaq"; info->cpu_flags = detect_cpu_flags(); info->num_threads = 0;