vp9_skin_detection.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. // Fixed-point skin color model parameters.
  16. static const int skin_mean[2] = {7463, 9614}; // q6
  17. static const int skin_inv_cov[4] = {4107, 1663, 1663, 2157}; // q16
  18. static const int skin_threshold = 1570636; // q18
  19. // Thresholds on luminance.
  20. static const int y_low = 20;
  21. static const int y_high = 220;
  22. // Evaluates the Mahalanobis distance measure for the input CbCr values.
  23. static int evaluate_skin_color_difference(int cb, int cr) {
  24. const int cb_q6 = cb << 6;
  25. const int cr_q6 = cr << 6;
  26. const int cb_diff_q12 = (cb_q6 - skin_mean[0]) * (cb_q6 - skin_mean[0]);
  27. const int cbcr_diff_q12 = (cb_q6 - skin_mean[0]) * (cr_q6 - skin_mean[1]);
  28. const int cr_diff_q12 = (cr_q6 - skin_mean[1]) * (cr_q6 - skin_mean[1]);
  29. const int cb_diff_q2 = (cb_diff_q12 + (1 << 9)) >> 10;
  30. const int cbcr_diff_q2 = (cbcr_diff_q12 + (1 << 9)) >> 10;
  31. const int cr_diff_q2 = (cr_diff_q12 + (1 << 9)) >> 10;
  32. const int skin_diff = skin_inv_cov[0] * cb_diff_q2 +
  33. skin_inv_cov[1] * cbcr_diff_q2 +
  34. skin_inv_cov[2] * cbcr_diff_q2 +
  35. skin_inv_cov[3] * cr_diff_q2;
  36. return skin_diff;
  37. }
  38. int vp9_skin_pixel(const uint8_t y, const uint8_t cb, const uint8_t cr) {
  39. if (y < y_low || y > y_high)
  40. return 0;
  41. else
  42. return (evaluate_skin_color_difference(cb, cr) < skin_threshold);
  43. }
  44. #ifdef OUTPUT_YUV_SKINMAP
  45. // For viewing skin map on input source.
  46. void vp9_compute_skin_map(VP9_COMP *const cpi, FILE *yuv_skinmap_file) {
  47. int i, j, mi_row, mi_col;
  48. VP9_COMMON *const cm = &cpi->common;
  49. uint8_t *y;
  50. const uint8_t *src_y = cpi->Source->y_buffer;
  51. const uint8_t *src_u = cpi->Source->u_buffer;
  52. const uint8_t *src_v = cpi->Source->v_buffer;
  53. const int src_ystride = cpi->Source->y_stride;
  54. const int src_uvstride = cpi->Source->uv_stride;
  55. YV12_BUFFER_CONFIG skinmap;
  56. memset(&skinmap, 0, sizeof(YV12_BUFFER_CONFIG));
  57. if (vp9_alloc_frame_buffer(&skinmap, cm->width, cm->height,
  58. cm->subsampling_x, cm->subsampling_y,
  59. VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment)) {
  60. vp9_free_frame_buffer(&skinmap);
  61. return;
  62. }
  63. memset(skinmap.buffer_alloc, 128, skinmap.frame_size);
  64. y = skinmap.y_buffer;
  65. // Loop through 8x8 blocks and set skin map based on center pixel of block.
  66. // Set y to white for skin block, otherwise set to source with gray scale.
  67. // Ignore rightmost/bottom boundary blocks.
  68. for (mi_row = 0; mi_row < cm->mi_rows - 1; ++mi_row) {
  69. for (mi_col = 0; mi_col < cm->mi_cols - 1; ++mi_col) {
  70. // Use middle pixel for each 8x8 block for skin detection.
  71. // If middle pixel is skin, assign whole 8x8 block to skin.
  72. const uint8_t ysource = src_y[4 * src_ystride + 4];
  73. const uint8_t usource = src_u[2 * src_uvstride + 2];
  74. const uint8_t vsource = src_v[2 * src_uvstride + 2];
  75. const int is_skin = vp9_skin_pixel(ysource, usource, vsource);
  76. for (i = 0; i < 8; i++) {
  77. for (j = 0; j < 8; j++) {
  78. if (is_skin)
  79. y[i * src_ystride + j] = 255;
  80. else
  81. y[i * src_ystride + j] = src_y[i * src_ystride + j];
  82. }
  83. }
  84. y += 8;
  85. src_y += 8;
  86. src_u += 4;
  87. src_v += 4;
  88. }
  89. y += (src_ystride << 3) - ((cm->mi_cols - 1) << 3);
  90. src_y += (src_ystride << 3) - ((cm->mi_cols - 1) << 3);
  91. src_u += (src_uvstride << 2) - ((cm->mi_cols - 1) << 2);
  92. src_v += (src_uvstride << 2) - ((cm->mi_cols - 1) << 2);
  93. }
  94. vp9_write_yuv_frame_420(&skinmap, yuv_skinmap_file);
  95. vp9_free_frame_buffer(&skinmap);
  96. }
  97. #endif