vp9_skin_detection.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright (c) 2015 The WebM project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include <limits.h>
  11. #include <math.h>
  12. #include "vp9/common/vp9_blockd.h"
  13. #include "vp9/encoder/vp9_encoder.h"
  14. #include "vp9/encoder/vp9_skin_detection.h"
  15. #define MODEL_MODE 1
  16. // Fixed-point skin color model parameters.
  17. static const int skin_mean[5][2] = { { 7463, 9614 },
  18. { 6400, 10240 },
  19. { 7040, 10240 },
  20. { 8320, 9280 },
  21. { 6800, 9614 } };
  22. static const int skin_inv_cov[4] = { 4107, 1663, 1663, 2157 }; // q16
  23. static const int skin_threshold[6] = { 1570636, 1400000, 800000,
  24. 800000, 800000, 800000 }; // q18
  25. // Thresholds on luminance.
  26. static const int y_low = 40;
  27. static const int y_high = 220;
  28. // Evaluates the Mahalanobis distance measure for the input CbCr values.
  29. static int evaluate_skin_color_difference(int cb, int cr, int idx) {
  30. const int cb_q6 = cb << 6;
  31. const int cr_q6 = cr << 6;
  32. const int cb_diff_q12 =
  33. (cb_q6 - skin_mean[idx][0]) * (cb_q6 - skin_mean[idx][0]);
  34. const int cbcr_diff_q12 =
  35. (cb_q6 - skin_mean[idx][0]) * (cr_q6 - skin_mean[idx][1]);
  36. const int cr_diff_q12 =
  37. (cr_q6 - skin_mean[idx][1]) * (cr_q6 - skin_mean[idx][1]);
  38. const int cb_diff_q2 = (cb_diff_q12 + (1 << 9)) >> 10;
  39. const int cbcr_diff_q2 = (cbcr_diff_q12 + (1 << 9)) >> 10;
  40. const int cr_diff_q2 = (cr_diff_q12 + (1 << 9)) >> 10;
  41. const int skin_diff =
  42. skin_inv_cov[0] * cb_diff_q2 + skin_inv_cov[1] * cbcr_diff_q2 +
  43. skin_inv_cov[2] * cbcr_diff_q2 + skin_inv_cov[3] * cr_diff_q2;
  44. return skin_diff;
  45. }
  46. int vp9_skin_pixel(const uint8_t y, const uint8_t cb, const uint8_t cr,
  47. int motion) {
  48. if (y < y_low || y > y_high) {
  49. return 0;
  50. } else {
  51. if (MODEL_MODE == 0) {
  52. return (evaluate_skin_color_difference(cb, cr, 0) < skin_threshold[0]);
  53. } else {
  54. int i = 0;
  55. // Exit on grey.
  56. if (cb == 128 && cr == 128) return 0;
  57. // Exit on very strong cb.
  58. if (cb > 150 && cr < 110) return 0;
  59. for (; i < 5; i++) {
  60. int skin_color_diff = evaluate_skin_color_difference(cb, cr, i);
  61. if (skin_color_diff < skin_threshold[i + 1]) {
  62. if (y < 60 && skin_color_diff > 3 * (skin_threshold[i + 1] >> 2))
  63. return 0;
  64. else if (motion == 0 &&
  65. skin_color_diff > (skin_threshold[i + 1] >> 1))
  66. return 0;
  67. else
  68. return 1;
  69. }
  70. // Exit if difference is much large than the threshold.
  71. if (skin_color_diff > (skin_threshold[i + 1] << 3)) {
  72. return 0;
  73. }
  74. }
  75. return 0;
  76. }
  77. }
  78. }
  79. int vp9_compute_skin_block(const uint8_t *y, const uint8_t *u, const uint8_t *v,
  80. int stride, int strideuv, int bsize,
  81. int consec_zeromv, int curr_motion_magn) {
  82. // No skin if block has been zero/small motion for long consecutive time.
  83. if (consec_zeromv > 60 && curr_motion_magn == 0) {
  84. return 0;
  85. } else {
  86. int motion = 1;
  87. // Take center pixel in block to determine is_skin.
  88. const int y_width_shift = (4 << b_width_log2_lookup[bsize]) >> 1;
  89. const int y_height_shift = (4 << b_height_log2_lookup[bsize]) >> 1;
  90. const int uv_width_shift = y_width_shift >> 1;
  91. const int uv_height_shift = y_height_shift >> 1;
  92. const uint8_t ysource = y[y_height_shift * stride + y_width_shift];
  93. const uint8_t usource = u[uv_height_shift * strideuv + uv_width_shift];
  94. const uint8_t vsource = v[uv_height_shift * strideuv + uv_width_shift];
  95. if (consec_zeromv > 25 && curr_motion_magn == 0) motion = 0;
  96. return vp9_skin_pixel(ysource, usource, vsource, motion);
  97. }
  98. }
  99. #ifdef OUTPUT_YUV_SKINMAP
  100. // For viewing skin map on input source.
  101. void vp9_compute_skin_map(VP9_COMP *const cpi, FILE *yuv_skinmap_file) {
  102. int i, j, mi_row, mi_col, num_bl;
  103. VP9_COMMON *const cm = &cpi->common;
  104. uint8_t *y;
  105. const uint8_t *src_y = cpi->Source->y_buffer;
  106. const uint8_t *src_u = cpi->Source->u_buffer;
  107. const uint8_t *src_v = cpi->Source->v_buffer;
  108. const int src_ystride = cpi->Source->y_stride;
  109. const int src_uvstride = cpi->Source->uv_stride;
  110. int y_bsize = 16; // Use 8x8 or 16x16.
  111. int uv_bsize = y_bsize >> 1;
  112. int ypos = y_bsize >> 1;
  113. int uvpos = uv_bsize >> 1;
  114. int shy = (y_bsize == 8) ? 3 : 4;
  115. int shuv = shy - 1;
  116. int fac = y_bsize / 8;
  117. // Use center pixel or average of center 2x2 pixels.
  118. int mode_filter = 0;
  119. YV12_BUFFER_CONFIG skinmap;
  120. memset(&skinmap, 0, sizeof(YV12_BUFFER_CONFIG));
  121. if (vpx_alloc_frame_buffer(&skinmap, cm->width, cm->height, cm->subsampling_x,
  122. cm->subsampling_y, VP9_ENC_BORDER_IN_PIXELS,
  123. cm->byte_alignment)) {
  124. vpx_free_frame_buffer(&skinmap);
  125. return;
  126. }
  127. memset(skinmap.buffer_alloc, 128, skinmap.frame_size);
  128. y = skinmap.y_buffer;
  129. // Loop through blocks and set skin map based on center pixel of block.
  130. // Set y to white for skin block, otherwise set to source with gray scale.
  131. // Ignore rightmost/bottom boundary blocks.
  132. for (mi_row = 0; mi_row < cm->mi_rows - 1; mi_row += fac) {
  133. num_bl = 0;
  134. for (mi_col = 0; mi_col < cm->mi_cols - 1; mi_col += fac) {
  135. int is_skin = 0;
  136. if (mode_filter == 1) {
  137. // Use 2x2 average at center.
  138. uint8_t ysource = src_y[ypos * src_ystride + ypos];
  139. uint8_t usource = src_u[uvpos * src_uvstride + uvpos];
  140. uint8_t vsource = src_v[uvpos * src_uvstride + uvpos];
  141. uint8_t ysource2 = src_y[(ypos + 1) * src_ystride + ypos];
  142. uint8_t usource2 = src_u[(uvpos + 1) * src_uvstride + uvpos];
  143. uint8_t vsource2 = src_v[(uvpos + 1) * src_uvstride + uvpos];
  144. uint8_t ysource3 = src_y[ypos * src_ystride + (ypos + 1)];
  145. uint8_t usource3 = src_u[uvpos * src_uvstride + (uvpos + 1)];
  146. uint8_t vsource3 = src_v[uvpos * src_uvstride + (uvpos + 1)];
  147. uint8_t ysource4 = src_y[(ypos + 1) * src_ystride + (ypos + 1)];
  148. uint8_t usource4 = src_u[(uvpos + 1) * src_uvstride + (uvpos + 1)];
  149. uint8_t vsource4 = src_v[(uvpos + 1) * src_uvstride + (uvpos + 1)];
  150. ysource = (ysource + ysource2 + ysource3 + ysource4) >> 2;
  151. usource = (usource + usource2 + usource3 + usource4) >> 2;
  152. vsource = (vsource + vsource2 + vsource3 + vsource4) >> 2;
  153. is_skin = vp9_skin_pixel(ysource, usource, vsource, 1);
  154. } else {
  155. int block_size = BLOCK_8X8;
  156. int consec_zeromv = 0;
  157. int bl_index = mi_row * cm->mi_cols + mi_col;
  158. int bl_index1 = bl_index + 1;
  159. int bl_index2 = bl_index + cm->mi_cols;
  160. int bl_index3 = bl_index2 + 1;
  161. if (y_bsize == 8)
  162. consec_zeromv = cpi->consec_zero_mv[bl_index];
  163. else
  164. consec_zeromv =
  165. VPXMIN(cpi->consec_zero_mv[bl_index],
  166. VPXMIN(cpi->consec_zero_mv[bl_index1],
  167. VPXMIN(cpi->consec_zero_mv[bl_index2],
  168. cpi->consec_zero_mv[bl_index3])));
  169. if (y_bsize == 16) block_size = BLOCK_16X16;
  170. is_skin =
  171. vp9_compute_skin_block(src_y, src_u, src_v, src_ystride,
  172. src_uvstride, block_size, consec_zeromv, 0);
  173. }
  174. for (i = 0; i < y_bsize; i++) {
  175. for (j = 0; j < y_bsize; j++) {
  176. if (is_skin)
  177. y[i * src_ystride + j] = 255;
  178. else
  179. y[i * src_ystride + j] = src_y[i * src_ystride + j];
  180. }
  181. }
  182. num_bl++;
  183. y += y_bsize;
  184. src_y += y_bsize;
  185. src_u += uv_bsize;
  186. src_v += uv_bsize;
  187. }
  188. y += (src_ystride << shy) - (num_bl << shy);
  189. src_y += (src_ystride << shy) - (num_bl << shy);
  190. src_u += (src_uvstride << shuv) - (num_bl << shuv);
  191. src_v += (src_uvstride << shuv) - (num_bl << shuv);
  192. }
  193. vp9_write_yuv_frame_420(&skinmap, yuv_skinmap_file);
  194. vpx_free_frame_buffer(&skinmap);
  195. }
  196. #endif