Coverage Report

Created: 2024-09-06 07:53

/src/libvpx/vpx_dsp/skin_detection.c
Line
Count
Source (jump to first uncovered line)
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
1.84M
#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
1.90M
                                              const int idx) {
31
1.90M
  const int cb_q6 = cb << 6;
32
1.90M
  const int cr_q6 = cr << 6;
33
1.90M
  const int cb_diff_q12 =
34
1.90M
      (cb_q6 - skin_mean[idx][0]) * (cb_q6 - skin_mean[idx][0]);
35
1.90M
  const int cbcr_diff_q12 =
36
1.90M
      (cb_q6 - skin_mean[idx][0]) * (cr_q6 - skin_mean[idx][1]);
37
1.90M
  const int cr_diff_q12 =
38
1.90M
      (cr_q6 - skin_mean[idx][1]) * (cr_q6 - skin_mean[idx][1]);
39
1.90M
  const int cb_diff_q2 = (cb_diff_q12 + (1 << 9)) >> 10;
40
1.90M
  const int cbcr_diff_q2 = (cbcr_diff_q12 + (1 << 9)) >> 10;
41
1.90M
  const int cr_diff_q2 = (cr_diff_q12 + (1 << 9)) >> 10;
42
1.90M
  const int skin_diff =
43
1.90M
      skin_inv_cov[0] * cb_diff_q2 + skin_inv_cov[1] * cbcr_diff_q2 +
44
1.90M
      skin_inv_cov[2] * cbcr_diff_q2 + skin_inv_cov[3] * cr_diff_q2;
45
1.90M
  return skin_diff;
46
1.90M
}
47
48
// Checks if the input yCbCr values corresponds to skin color.
49
5.09M
int vpx_skin_pixel(const int y, const int cb, const int cr, int motion) {
50
5.09M
  if (y < y_low || y > y_high) {
51
3.24M
    return 0;
52
3.24M
  } else if (MODEL_MODE == 0) {
53
0
    return (vpx_evaluate_skin_color_difference(cb, cr, 0) < skin_threshold[0]);
54
1.84M
  } else {
55
1.84M
    int i = 0;
56
    // Exit on grey.
57
1.84M
    if (cb == 128 && cr == 128) return 0;
58
    // Exit on very strong cb.
59
1.84M
    if (cb > 150 && cr < 110) return 0;
60
1.90M
    for (; i < 5; ++i) {
61
1.90M
      int skin_color_diff = vpx_evaluate_skin_color_difference(cb, cr, i);
62
1.90M
      if (skin_color_diff < skin_threshold[i + 1]) {
63
20.7k
        if (y < 60 && skin_color_diff > 3 * (skin_threshold[i + 1] >> 2)) {
64
1.43k
          return 0;
65
19.3k
        } else if (motion == 0 &&
66
19.3k
                   skin_color_diff > (skin_threshold[i + 1] >> 1)) {
67
13
          return 0;
68
19.2k
        } else {
69
19.2k
          return 1;
70
19.2k
        }
71
20.7k
      }
72
      // Exit if difference is much large than the threshold.
73
1.87M
      if (skin_color_diff > (skin_threshold[i + 1] << 3)) {
74
1.71M
        return 0;
75
1.71M
      }
76
1.87M
    }
77
1.50k
    return 0;
78
1.73M
  }
79
5.09M
}