2 * Copyright (c) 2017 The WebM project authors. All Rights Reserved.
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.
11 #include "vpx_dsp/skin_detection.h"
15 // Fixed-point skin color model parameters.
16 static const int skin_mean[5][2] = { { 7463, 9614 },
21 static const int skin_inv_cov[4] = { 4107, 1663, 1663, 2157 }; // q16
22 static const int skin_threshold[6] = { 1570636, 1400000, 800000,
23 800000, 800000, 800000 }; // q18
24 // Thresholds on luminance.
25 static const int y_low = 40;
26 static const int y_high = 220;
28 // Evaluates the Mahalanobis distance measure for the input CbCr values.
29 static int vpx_evaluate_skin_color_difference(const int cb, const int cr,
31 const int cb_q6 = cb << 6;
32 const int cr_q6 = cr << 6;
33 const int cb_diff_q12 =
34 (cb_q6 - skin_mean[idx][0]) * (cb_q6 - skin_mean[idx][0]);
35 const int cbcr_diff_q12 =
36 (cb_q6 - skin_mean[idx][0]) * (cr_q6 - skin_mean[idx][1]);
37 const int cr_diff_q12 =
38 (cr_q6 - skin_mean[idx][1]) * (cr_q6 - skin_mean[idx][1]);
39 const int cb_diff_q2 = (cb_diff_q12 + (1 << 9)) >> 10;
40 const int cbcr_diff_q2 = (cbcr_diff_q12 + (1 << 9)) >> 10;
41 const int cr_diff_q2 = (cr_diff_q12 + (1 << 9)) >> 10;
43 skin_inv_cov[0] * cb_diff_q2 + skin_inv_cov[1] * cbcr_diff_q2 +
44 skin_inv_cov[2] * cbcr_diff_q2 + skin_inv_cov[3] * cr_diff_q2;
48 // Checks if the input yCbCr values corresponds to skin color.
49 int vpx_skin_pixel(const int y, const int cb, const int cr, int motion) {
50 if (y < y_low || y > y_high) {
52 } else if (MODEL_MODE == 0) {
53 return (vpx_evaluate_skin_color_difference(cb, cr, 0) < skin_threshold[0]);
57 if (cb == 128 && cr == 128) return 0;
58 // Exit on very strong cb.
59 if (cb > 150 && cr < 110) return 0;
61 int skin_color_diff = vpx_evaluate_skin_color_difference(cb, cr, i);
62 if (skin_color_diff < skin_threshold[i + 1]) {
63 if (y < 60 && skin_color_diff > 3 * (skin_threshold[i + 1] >> 2)) {
65 } else if (motion == 0 &&
66 skin_color_diff > (skin_threshold[i + 1] >> 1)) {
72 // Exit if difference is much large than the threshold.
73 if (skin_color_diff > (skin_threshold[i + 1] << 3)) {