Advertisement
Guest User

Dark Shikari

a guest
Feb 21st, 2008
4,295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.95 KB | None | 0 0
  1. --- /c/Documents and Settings/Jason/Desktop/plugin_lumimasking.c 2006-05-05 21:37:15.000000000 -0700
  2. +++ ../xvid/src/plugins/plugin_lumimasking.c 2008-02-21 02:16:56.359375000 -0800
  3. @@ -2,11 +2,11 @@
  4. *
  5. * XVID MPEG-4 VIDEO CODEC
  6. * - XviD plugin: performs a lumimasking algorithm on encoded frame -
  7. *
  8. * Copyright(C) 2002-2003 Peter Ross <pross@xvid.org>
  9. - * 2002 Christoph Lampert <gruel@web.de>
  10. + * 2002 Christoph Lampert <gruel@web.de>
  11. *
  12. * This program is free software ; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation ; either version 2 of the License, or
  15. * (at your option) any later version.
  16. @@ -23,10 +23,12 @@
  17. * $Id: plugin_lumimasking.c,v 1.6 2006/05/06 04:37:15 syskin Exp $
  18. *
  19. ****************************************************************************/
  20.  
  21. #include <stdlib.h>
  22. +#include <stdio.h>
  23. +#include <math.h>
  24.  
  25. #include "../xvid.h"
  26. #include "../global.h"
  27. #include "../portab.h"
  28. #include "../utils/emms.h"
  29. @@ -155,31 +157,31 @@
  30. int *out,
  31. int num,
  32. int min_quant,
  33. int max_quant);
  34.  
  35. -static int
  36. -lumi_plg_frame(lumi_data_t *handle, xvid_plg_data_t *data)
  37. +static int lumi_plg_frame(lumi_data_t *handle, xvid_plg_data_t *data)
  38. {
  39. - int i, j;
  40. -
  41. - float global = 0.0f;
  42. + //Don't apply variance-masking to B-frames.
  43. + fprintf(stderr,"TEST\n");
  44. + return 0;
  45. + if (data->type == XVID_TYPE_BVOP) return 0;
  46.  
  47. - const float DarkAmpl = 14 / 4;
  48. - const float BrightAmpl = 10 / 3;
  49. - float DarkThres = 90;
  50. - float BrightThres = 200;
  51. + int i, j;
  52.  
  53. - const float GlobalDarkThres = 60;
  54. - const float GlobalBrightThres = 170;
  55. + /* Arbitrary centerpoint for variance-based AQ. Roughly the same as used in x264. */
  56. + float center = 14000;
  57.  
  58. - if (data->type == XVID_TYPE_BVOP) return 0;
  59. + /* Arbitrary strength for variance-based AQ. */
  60. + float strength = 0.2;
  61.  
  62. /* Do this for all macroblocks individually */
  63. - for (j = 0; j < data->mb_height; j++) {
  64. - for (i = 0; i < data->mb_width; i++) {
  65. - int k, l, sum = 0;
  66. + for (j = 0; j < data->mb_height; j++)
  67. + {
  68. + for (i = 0; i < data->mb_width; i++)
  69. + {
  70. + int k, l, sum = 0, sum_of_squares = 0;
  71. unsigned char *ptr;
  72.  
  73. /* Initialize the current quant value to the frame quant */
  74. handle->quant[j*data->mb_width + i] = (float)data->quant;
  75.  
  76. @@ -187,41 +189,34 @@
  77.  
  78. /* Get the MB address */
  79. ptr = data->current.plane[0];
  80. ptr += 16*j*data->current.stride[0] + 16*i;
  81.  
  82. - /* Accumulate luminance */
  83. + /* Accumulate sum and sum of squares over the MB */
  84. for (k = 0; k < 16; k++)
  85. for (l = 0; l < 16; l++)
  86. - sum += ptr[k*data->current.stride[0] + l];
  87. + {
  88. + int val = ptr[k*data->current.stride[0] + l];
  89. + sum += val;
  90. + sum_of_squares += val * val;
  91. + }
  92. +
  93. + /* Variance = SSD - SAD^2 / (numpixels) */
  94. + int variance = sum_of_squares - sum * sum / 256;
  95.  
  96. - handle->val[j*data->mb_width + i] = (float)sum/256.0f;
  97. -
  98. - /* Accumulate the global frame luminance */
  99. - global += (float)sum/256.0f;
  100. + handle->val[j*data->mb_width + i] = variance;
  101. }
  102. }
  103. -
  104. - /* Normalize the global luminance accumulator */
  105. - global /= data->mb_width*data->mb_height;
  106. -
  107. - DarkThres = DarkThres*global/127.0f;
  108. - BrightThres = BrightThres*global/127.0f;
  109. -
  110. -
  111. - /* Apply luminance masking only to frames where the global luminance is
  112. - * higher than DarkThreshold and lower than Bright Threshold */
  113. - if ((global < GlobalBrightThres) && (global > GlobalDarkThres)) {
  114. -
  115. - /* Apply the luminance masking formulas to all MBs */
  116. - for (i = 0; i < data->mb_height; i++) {
  117. - for (j = 0; j < data->mb_width; j++) {
  118. - if (handle->val[i*data->mb_width + j] < DarkThres)
  119. - handle->quant[i*data->mb_width + j] *= 1 + DarkAmpl * (DarkThres - handle->val[i*data->mb_width + j]) / DarkThres;
  120. - else if (handle->val[i*data->mb_width + j] > BrightThres)
  121. - handle->quant[i*data->mb_width + j] *= 1 + BrightAmpl * (handle->val[i*data->mb_width + j] - BrightThres) / (255 - BrightThres);
  122. - }
  123. +
  124. + /* Apply the variance masking formula to all MBs */
  125. + for (i = 0; i < data->mb_height; i++)
  126. + {
  127. + for (j = 0; j < data->mb_width; j++)
  128. + {
  129. + float value = handle->val[i*data->mb_width + j];
  130. + float qscale_diff = strength * logf(value / center);
  131. + handle->quant[i*data->mb_width + j] *= (1.0 + qscale_diff);
  132. }
  133. }
  134.  
  135. /* Normalize the quantizer field */
  136. data->quant = normalize_quantizer_field(handle->quant,
  137. @@ -246,11 +241,11 @@
  138.  
  139. /*****************************************************************************
  140. * Helper functions
  141. ****************************************************************************/
  142.  
  143. -#define RDIFF(a, b) ((int)(a+0.5)-(int)(b+0.5))
  144. +#define RDIFF(a, b) ((int)(a+0.5)-(int)(b+0.5))
  145.  
  146. static int
  147. normalize_quantizer_field(float *in,
  148. int *out,
  149. int num,
  150.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement