]> granicus.if.org Git - libvpx/blob - vp8/common/skin_detection.c
Merge "ppc: Add vpx_sadnxmx4d_vsx for n,m = {8, 16, 32 ,64}"
[libvpx] / vp8 / common / skin_detection.c
1 /*
2  *  Copyright (c) 2015 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 "vp8/common/skin_detection.h"
12 #include "vp8/common/alloccommon.h"
13 #include "vpx_dsp/vpx_dsp_common.h"
14 #include "vpx_mem/vpx_mem.h"
15
16 #define MODEL_MODE 1
17
18 // Fixed-point skin color model parameters.
19 static const int skin_mean[5][2] = { { 7463, 9614 },
20                                      { 6400, 10240 },
21                                      { 7040, 10240 },
22                                      { 8320, 9280 },
23                                      { 6800, 9614 } };
24 static const int skin_inv_cov[4] = { 4107, 1663, 1663, 2157 };  // q16
25 static const int skin_threshold[6] = { 1570636, 1400000, 800000,
26                                        800000,  800000,  800000 };  // q18
27
28 // Thresholds on luminance.
29 static const int y_low = 40;
30 static const int y_high = 220;
31
32 // Evaluates the Mahalanobis distance measure for the input CbCr values.
33 static int evaluate_skin_color_difference(const int cb, const int cr,
34                                           const int idx) {
35   const int cb_q6 = cb << 6;
36   const int cr_q6 = cr << 6;
37   const int cb_diff_q12 =
38       (cb_q6 - skin_mean[idx][0]) * (cb_q6 - skin_mean[idx][0]);
39   const int cbcr_diff_q12 =
40       (cb_q6 - skin_mean[idx][0]) * (cr_q6 - skin_mean[idx][1]);
41   const int cr_diff_q12 =
42       (cr_q6 - skin_mean[idx][1]) * (cr_q6 - skin_mean[idx][1]);
43   const int cb_diff_q2 = (cb_diff_q12 + (1 << 9)) >> 10;
44   const int cbcr_diff_q2 = (cbcr_diff_q12 + (1 << 9)) >> 10;
45   const int cr_diff_q2 = (cr_diff_q12 + (1 << 9)) >> 10;
46   const int skin_diff =
47       skin_inv_cov[0] * cb_diff_q2 + skin_inv_cov[1] * cbcr_diff_q2 +
48       skin_inv_cov[2] * cbcr_diff_q2 + skin_inv_cov[3] * cr_diff_q2;
49   return skin_diff;
50 }
51
52 // Checks if the input yCbCr values corresponds to skin color.
53 int skin_pixel(int y, int cb, int cr, int motion) {
54   if (y < y_low || y > y_high) {
55     return 0;
56   } else {
57     if (MODEL_MODE == 0) {
58       return (evaluate_skin_color_difference(cb, cr, 0) < skin_threshold[0]);
59     } else {
60       int i = 0;
61       // Exit on grey.
62       if (cb == 128 && cr == 128) return 0;
63       // Exit on very strong cb.
64       if (cb > 150 && cr < 110) return 0;
65       for (; i < 5; ++i) {
66         int skin_color_diff = evaluate_skin_color_difference(cb, cr, i);
67         if (skin_color_diff < skin_threshold[i + 1]) {
68           if (y < 60 && skin_color_diff > 3 * (skin_threshold[i + 1] >> 2)) {
69             return 0;
70           } else if (motion == 0 &&
71                      skin_color_diff > (skin_threshold[i + 1] >> 1)) {
72             return 0;
73           } else {
74             return 1;
75           }
76         }
77         // Exit if difference is much large than the threshold.
78         if (skin_color_diff > (skin_threshold[i + 1] << 3)) {
79           return 0;
80         }
81       }
82       return 0;
83     }
84   }
85 }
86
87 int compute_skin_block(const uint8_t *y, const uint8_t *u, const uint8_t *v,
88                        int stride, int strideuv, int consec_zeromv,
89                        int curr_motion_magn) {
90   // No skin if block has been zero/small motion for long consecutive time.
91   if (consec_zeromv > 60 && curr_motion_magn == 0) {
92     return 0;
93   } else {
94     int motion = 1;
95     // Take the average of center 2x2 pixels.
96     const int ysource = (y[7 * stride + 7] + y[7 * stride + 8] +
97                          y[8 * stride + 7] + y[8 * stride + 8]) >>
98                         2;
99     const int usource = (u[3 * strideuv + 3] + u[3 * strideuv + 4] +
100                          u[4 * strideuv + 3] + u[4 * strideuv + 4]) >>
101                         2;
102     const int vsource = (v[3 * strideuv + 3] + v[3 * strideuv + 4] +
103                          v[4 * strideuv + 3] + v[4 * strideuv + 4]) >>
104                         2;
105     if (consec_zeromv > 25 && curr_motion_magn == 0) motion = 0;
106     return skin_pixel(ysource, usource, vsource, motion);
107   }
108 }
109
110 #ifdef OUTPUT_YUV_SKINMAP
111 // For viewing skin map on input source.
112 void compute_skin_map(VP8_COMP *const cpi, FILE *yuv_skinmap_file) {
113   int i, j, mb_row, mb_col, num_bl;
114   VP8_COMMON *const cm = &cpi->common;
115   uint8_t *y;
116   const uint8_t *src_y = cpi->Source->y_buffer;
117   const uint8_t *src_u = cpi->Source->u_buffer;
118   const uint8_t *src_v = cpi->Source->v_buffer;
119   const int src_ystride = cpi->Source->y_stride;
120   const int src_uvstride = cpi->Source->uv_stride;
121
122   YV12_BUFFER_CONFIG skinmap;
123   memset(&skinmap, 0, sizeof(skinmap));
124   if (vp8_yv12_alloc_frame_buffer(&skinmap, cm->Width, cm->Height,
125                                   VP8BORDERINPIXELS) < 0) {
126     vpx_free_frame_buffer(&skinmap);
127     return;
128   }
129   memset(skinmap.buffer_alloc, 128, skinmap.frame_size);
130   y = skinmap.y_buffer;
131   // Loop through blocks and set skin map based on center pixel of block.
132   // Set y to white for skin block, otherwise set to source with gray scale.
133   // Ignore rightmost/bottom boundary blocks.
134   for (mb_row = 0; mb_row < cm->mb_rows; mb_row += 1) {
135     num_bl = 0;
136     for (mb_col = 0; mb_col < cm->mb_cols; mb_col += 1) {
137       int is_skin = 0;
138       int consec_zeromv = 0;
139       const int bl_index = mb_row * cm->mb_cols + mb_col;
140       const int bl_index1 = bl_index + 1;
141       const int bl_index2 = bl_index + cm->mb_cols;
142       const int bl_index3 = bl_index2 + 1;
143       consec_zeromv = VPXMIN(cpi->consec_zero_last[bl_index],
144                              VPXMIN(cpi->consec_zero_last[bl_index1],
145                                     VPXMIN(cpi->consec_zero_last[bl_index2],
146                                            cpi->consec_zero_last[bl_index3])));
147       is_skin = compute_skin_block(src_y, src_u, src_v, src_ystride,
148                                    src_uvstride, consec_zeromv, 0);
149       for (i = 0; i < 16; i++) {
150         for (j = 0; j < 16; j++) {
151           if (is_skin)
152             y[i * src_ystride + j] = 255;
153           else
154             y[i * src_ystride + j] = src_y[i * src_ystride + j];
155         }
156       }
157       num_bl++;
158       y += 16;
159       src_y += 16;
160       src_u += 8;
161       src_v += 8;
162     }
163     y += (src_ystride << 4) - (num_bl << 4);
164     src_y += (src_ystride << 4) - (num_bl << 4);
165     src_u += (src_uvstride << 3) - (num_bl << 3);
166     src_v += (src_uvstride << 3) - (num_bl << 3);
167   }
168   vp8_write_yuv_frame(yuv_skinmap_file, &skinmap);
169   vpx_free_frame_buffer(&skinmap);
170 }
171 #endif  // OUTPUT_YUV_SKINMAP