• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2015 The WebRTC 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 
11 #include <limits.h>
12 #include <math.h>
13 
14 #include "webrtc/modules/video_processing/util/skin_detection.h"
15 
16 namespace webrtc {
17 
18 // Fixed-point skin color model parameters.
19 static const int skin_mean[2] = {7463, 9614};                 // q6
20 static const int skin_inv_cov[4] = {4107, 1663, 1663, 2157};  // q16
21 static const int skin_threshold = 1570636;                    // q18
22 
23 // Thresholds on luminance.
24 static const int y_low = 20;
25 static const int y_high = 220;
26 
27 // Evaluates the Mahalanobis distance measure for the input CbCr values.
EvaluateSkinColorDifference(int cb,int cr)28 static int EvaluateSkinColorDifference(int cb, int cr) {
29   const int cb_q6 = cb << 6;
30   const int cr_q6 = cr << 6;
31   const int cb_diff_q12 = (cb_q6 - skin_mean[0]) * (cb_q6 - skin_mean[0]);
32   const int cbcr_diff_q12 = (cb_q6 - skin_mean[0]) * (cr_q6 - skin_mean[1]);
33   const int cr_diff_q12 = (cr_q6 - skin_mean[1]) * (cr_q6 - skin_mean[1]);
34   const int cb_diff_q2 = (cb_diff_q12 + (1 << 9)) >> 10;
35   const int cbcr_diff_q2 = (cbcr_diff_q12 + (1 << 9)) >> 10;
36   const int cr_diff_q2 = (cr_diff_q12 + (1 << 9)) >> 10;
37   const int skin_diff =
38       skin_inv_cov[0] * cb_diff_q2 + skin_inv_cov[1] * cbcr_diff_q2 +
39       skin_inv_cov[2] * cbcr_diff_q2 + skin_inv_cov[3] * cr_diff_q2;
40   return skin_diff;
41 }
42 
MbHasSkinColor(const uint8_t * y_src,const uint8_t * u_src,const uint8_t * v_src,const int stride_y,const int stride_u,const int stride_v,const int mb_row,const int mb_col)43 bool MbHasSkinColor(const uint8_t* y_src,
44                     const uint8_t* u_src,
45                     const uint8_t* v_src,
46                     const int stride_y,
47                     const int stride_u,
48                     const int stride_v,
49                     const int mb_row,
50                     const int mb_col) {
51   const uint8_t* y = y_src + ((mb_row << 4) + 8) * stride_y + (mb_col << 4) + 8;
52   const uint8_t* u = u_src + ((mb_row << 3) + 4) * stride_u + (mb_col << 3) + 4;
53   const uint8_t* v = v_src + ((mb_row << 3) + 4) * stride_v + (mb_col << 3) + 4;
54   // Use 2x2 average of center pixel to compute skin area.
55   uint8_t y_avg = (*y + *(y + 1) + *(y + stride_y) + *(y + stride_y + 1)) >> 2;
56   uint8_t u_avg = (*u + *(u + 1) + *(u + stride_u) + *(u + stride_u + 1)) >> 2;
57   uint8_t v_avg = (*v + *(v + 1) + *(v + stride_v) + *(v + stride_v + 1)) >> 2;
58   // Ignore MB with too high or low brightness.
59   if (y_avg < y_low || y_avg > y_high)
60     return false;
61   else
62     return (EvaluateSkinColorDifference(u_avg, v_avg) < skin_threshold);
63 }
64 
65 }  // namespace webrtc
66