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 <limits.h>
12 #include <math.h>
13
14 #include "vp9/common/vp9_blockd.h"
15 #include "vp9/encoder/vp9_encoder.h"
16 #include "vp9/encoder/vp9_skin_detection.h"
17
18 // Fixed-point skin color model parameters.
19 static const int skin_mean[2] = {7463, 9614}; // q6
20 static const int skin_inv_cov[4] = {4107, 1663, 1663, 2157}; // q16
21 static const int skin_threshold = 1570636; // q18
22
23 // Thresholds on luminance.
24 static const int y_low = 20;
25 static const int y_high = 220;
26
27 // Evaluates the Mahalanobis distance measure for the input CbCr values.
evaluate_skin_color_difference(int cb,int cr)28 static int evaluate_skin_color_difference(int cb, int cr) {
29 const int cb_q6 = cb << 6;
30 const int cr_q6 = cr << 6;
31 const int cb_diff_q12 = (cb_q6 - skin_mean[0]) * (cb_q6 - skin_mean[0]);
32 const int cbcr_diff_q12 = (cb_q6 - skin_mean[0]) * (cr_q6 - skin_mean[1]);
33 const int cr_diff_q12 = (cr_q6 - skin_mean[1]) * (cr_q6 - skin_mean[1]);
34 const int cb_diff_q2 = (cb_diff_q12 + (1 << 9)) >> 10;
35 const int cbcr_diff_q2 = (cbcr_diff_q12 + (1 << 9)) >> 10;
36 const int cr_diff_q2 = (cr_diff_q12 + (1 << 9)) >> 10;
37 const int skin_diff = skin_inv_cov[0] * cb_diff_q2 +
38 skin_inv_cov[1] * cbcr_diff_q2 +
39 skin_inv_cov[2] * cbcr_diff_q2 +
40 skin_inv_cov[3] * cr_diff_q2;
41 return skin_diff;
42 }
43
vp9_skin_pixel(const uint8_t y,const uint8_t cb,const uint8_t cr)44 int vp9_skin_pixel(const uint8_t y, const uint8_t cb, const uint8_t cr) {
45 if (y < y_low || y > y_high)
46 return 0;
47 else
48 return (evaluate_skin_color_difference(cb, cr) < skin_threshold);
49 }
50
51 #ifdef OUTPUT_YUV_SKINMAP
52 // For viewing skin map on input source.
vp9_compute_skin_map(VP9_COMP * const cpi,FILE * yuv_skinmap_file)53 void vp9_compute_skin_map(VP9_COMP *const cpi, FILE *yuv_skinmap_file) {
54 int i, j, mi_row, mi_col, num_bl;
55 VP9_COMMON *const cm = &cpi->common;
56 uint8_t *y;
57 const uint8_t *src_y = cpi->Source->y_buffer;
58 const uint8_t *src_u = cpi->Source->u_buffer;
59 const uint8_t *src_v = cpi->Source->v_buffer;
60 const int src_ystride = cpi->Source->y_stride;
61 const int src_uvstride = cpi->Source->uv_stride;
62 int y_bsize = 16; // Use 8x8 or 16x16.
63 int uv_bsize = y_bsize >> 1;
64 int ypos = y_bsize >> 1;
65 int uvpos = uv_bsize >> 1;
66 int shy = (y_bsize == 8) ? 3 : 4;
67 int shuv = shy - 1;
68 int fac = y_bsize / 8;
69 // Use center pixel or average of center 2x2 pixels.
70 int mode_filter = 1;
71 YV12_BUFFER_CONFIG skinmap;
72 memset(&skinmap, 0, sizeof(YV12_BUFFER_CONFIG));
73 if (vpx_alloc_frame_buffer(&skinmap, cm->width, cm->height,
74 cm->subsampling_x, cm->subsampling_y,
75 VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment)) {
76 vpx_free_frame_buffer(&skinmap);
77 return;
78 }
79 memset(skinmap.buffer_alloc, 128, skinmap.frame_size);
80 y = skinmap.y_buffer;
81 // Loop through blocks and set skin map based on center pixel of block.
82 // Set y to white for skin block, otherwise set to source with gray scale.
83 // Ignore rightmost/bottom boundary blocks.
84 for (mi_row = 0; mi_row < cm->mi_rows - 1; mi_row += fac) {
85 num_bl = 0;
86 for (mi_col = 0; mi_col < cm->mi_cols - 1; mi_col += fac) {
87 // Select pixel for each block for skin detection.
88 // Use center pixel, or 2x2 average at center.
89 uint8_t ysource = src_y[ypos * src_ystride + ypos];
90 uint8_t usource = src_u[uvpos * src_uvstride + uvpos];
91 uint8_t vsource = src_v[uvpos * src_uvstride + uvpos];
92 uint8_t ysource2 = src_y[(ypos + 1) * src_ystride + ypos];
93 uint8_t usource2 = src_u[(uvpos + 1) * src_uvstride + uvpos];
94 uint8_t vsource2 = src_v[(uvpos + 1) * src_uvstride + uvpos];
95 uint8_t ysource3 = src_y[ypos * src_ystride + (ypos + 1)];
96 uint8_t usource3 = src_u[uvpos * src_uvstride + (uvpos + 1)];
97 uint8_t vsource3 = src_v[uvpos * src_uvstride + (uvpos + 1)];
98 uint8_t ysource4 = src_y[(ypos + 1) * src_ystride + (ypos + 1)];
99 uint8_t usource4 = src_u[(uvpos + 1) * src_uvstride + (uvpos + 1)];
100 uint8_t vsource4 = src_v[(uvpos + 1) * src_uvstride + (uvpos + 1)];
101 int is_skin = 0;
102 if (mode_filter == 1) {
103 ysource = (ysource + ysource2 + ysource3 + ysource4) >> 2;
104 usource = (usource + usource2 + usource3 + usource4) >> 2;
105 vsource = (vsource + vsource2 + vsource3 + vsource4) >> 2;
106 }
107 is_skin = vp9_skin_pixel(ysource, usource, vsource);
108 for (i = 0; i < y_bsize; i++) {
109 for (j = 0; j < y_bsize; j++) {
110 if (is_skin)
111 y[i * src_ystride + j] = 255;
112 else
113 y[i * src_ystride + j] = src_y[i * src_ystride + j];
114 }
115 }
116 num_bl++;
117 y += y_bsize;
118 src_y += y_bsize;
119 src_u += uv_bsize;
120 src_v += uv_bsize;
121 }
122 y += (src_ystride << shy) - (num_bl << shy);
123 src_y += (src_ystride << shy) - (num_bl << shy);
124 src_u += (src_uvstride << shuv) - (num_bl << shuv);
125 src_v += (src_uvstride << shuv) - (num_bl << shuv);
126 }
127 vp9_write_yuv_frame_420(&skinmap, yuv_skinmap_file);
128 vpx_free_frame_buffer(&skinmap);
129 }
130 #endif
131