1 /* 2 * Copyright (c) 2017 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 11 #include "vpx_dsp/skin_detection.h" 12 13 #define MODEL_MODE 1 14 15 // Fixed-point skin color model parameters. 16 static const int skin_mean[5][2] = { { 7463, 9614 }, 17 { 6400, 10240 }, 18 { 7040, 10240 }, 19 { 8320, 9280 }, 20 { 6800, 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; 27 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, 30 const int idx) { 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; 42 const int skin_diff = 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; 45 return skin_diff; 46 } 47 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) { 51 return 0; 52 } else if (MODEL_MODE == 0) { 53 return (vpx_evaluate_skin_color_difference(cb, cr, 0) < skin_threshold[0]); 54 } else { 55 int i = 0; 56 // Exit on grey. 57 if (cb == 128 && cr == 128) return 0; 58 // Exit on very strong cb. 59 if (cb > 150 && cr < 110) return 0; 60 for (; i < 5; ++i) { 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)) { 64 return 0; 65 } else if (motion == 0 && 66 skin_color_diff > (skin_threshold[i + 1] >> 1)) { 67 return 0; 68 } else { 69 return 1; 70 } 71 } 72 // Exit if difference is much large than the threshold. 73 if (skin_color_diff > (skin_threshold[i + 1] << 3)) { 74 return 0; 75 } 76 } 77 return 0; 78 } 79 } 80