blob: 76399d6e73438a0056231595557ec43e89e7fac0 [file] [log] [blame]
jackychen8f9902a2015-11-26 02:59:48 -08001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/video_processing/util/skin_detection.h"
jackychen8f9902a2015-11-26 02:59:48 -080012
13namespace webrtc {
14
15// Fixed-point skin color model parameters.
Yves Gerey665174f2018-06-19 15:03:05 +020016static const int skin_mean[5][2] = {{7463, 9614},
17 {6400, 10240},
18 {7040, 10240},
19 {8320, 9280},
20 {6800, 9614}};
jackychen8f9902a2015-11-26 02:59:48 -080021static const int skin_inv_cov[4] = {4107, 1663, 1663, 2157}; // q16
Yves Gerey665174f2018-06-19 15:03:05 +020022static const int skin_threshold[6] = {1570636, 1400000, 800000,
23 800000, 800000, 800000}; // q18
jackychen8f9902a2015-11-26 02:59:48 -080024
25// Thresholds on luminance.
jackychen3ad4bd32016-03-11 11:57:03 -080026static const int y_low = 40;
jackychen8f9902a2015-11-26 02:59:48 -080027static const int y_high = 220;
28
29// Evaluates the Mahalanobis distance measure for the input CbCr values.
jackychen3ad4bd32016-03-11 11:57:03 -080030static int EvaluateSkinColorDifference(int cb, int cr, int idx) {
jackychen8f9902a2015-11-26 02:59:48 -080031 const int cb_q6 = cb << 6;
32 const int cr_q6 = cr << 6;
jackychen3ad4bd32016-03-11 11:57:03 -080033 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]);
jackychen8f9902a2015-11-26 02:59:48 -080039 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;
Yves Gerey665174f2018-06-19 15:03:05 +020042 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;
jackychen8f9902a2015-11-26 02:59:48 -080045 return skin_diff;
46}
47
jackychen3ad4bd32016-03-11 11:57:03 -080048static int SkinPixel(const uint8_t y, const uint8_t cb, const uint8_t cr) {
49 if (y < y_low || y > y_high) {
50 return 0;
51 } else {
52 if (MODEL_MODE == 0) {
53 return (EvaluateSkinColorDifference(cb, cr, 0) < skin_threshold[0]);
54 } else {
55 // Exit on grey.
56 if (cb == 128 && cr == 128)
57 return 0;
58 // Exit on very strong cb.
59 if (cb > 150 && cr < 110)
60 return 0;
61 // Exit on (another) low luminance threshold if either color is high.
62 if (y < 50 && (cb > 140 || cr > 140))
63 return 0;
64 for (int i = 0; i < 5; i++) {
65 int diff = EvaluateSkinColorDifference(cb, cr, i);
66 if (diff < skin_threshold[i + 1]) {
67 return 1;
68 } else if (diff > (skin_threshold[i + 1] << 3)) {
69 // Exit if difference is much large than the threshold.
70 return 0;
71 }
72 }
73 return 0;
74 }
75 }
76}
77
jackychen8f9902a2015-11-26 02:59:48 -080078bool MbHasSkinColor(const uint8_t* y_src,
79 const uint8_t* u_src,
80 const uint8_t* v_src,
81 const int stride_y,
82 const int stride_u,
83 const int stride_v,
84 const int mb_row,
85 const int mb_col) {
mflodman99ab9442015-12-07 22:54:50 -080086 const uint8_t* y = y_src + ((mb_row << 4) + 8) * stride_y + (mb_col << 4) + 8;
87 const uint8_t* u = u_src + ((mb_row << 3) + 4) * stride_u + (mb_col << 3) + 4;
88 const uint8_t* v = v_src + ((mb_row << 3) + 4) * stride_v + (mb_col << 3) + 4;
jackychen8f9902a2015-11-26 02:59:48 -080089 // Use 2x2 average of center pixel to compute skin area.
mflodman99ab9442015-12-07 22:54:50 -080090 uint8_t y_avg = (*y + *(y + 1) + *(y + stride_y) + *(y + stride_y + 1)) >> 2;
91 uint8_t u_avg = (*u + *(u + 1) + *(u + stride_u) + *(u + stride_u + 1)) >> 2;
92 uint8_t v_avg = (*v + *(v + 1) + *(v + stride_v) + *(v + stride_v + 1)) >> 2;
jackychen3ad4bd32016-03-11 11:57:03 -080093 return SkinPixel(y_avg, u_avg, v_avg) == 1;
jackychen8f9902a2015-11-26 02:59:48 -080094}
95
96} // namespace webrtc