vp9_skin_detection.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. #include <limits.h>
  11. #include <math.h>
  12. #include "vp9/common/vp9_blockd.h"
  13. #include "vp9/encoder/vp9_encoder.h"
  14. #include "vp9/encoder/vp9_skin_detection.h"
  15. int vp9_compute_skin_block(const uint8_t *y, const uint8_t *u, const uint8_t *v,
  16. int stride, int strideuv, int bsize,
  17. int consec_zeromv, int curr_motion_magn) {
  18. // No skin if block has been zero/small motion for long consecutive time.
  19. if (consec_zeromv > 60 && curr_motion_magn == 0) {
  20. return 0;
  21. } else {
  22. int motion = 1;
  23. // Take center pixel in block to determine is_skin.
  24. const int y_width_shift = (4 << b_width_log2_lookup[bsize]) >> 1;
  25. const int y_height_shift = (4 << b_height_log2_lookup[bsize]) >> 1;
  26. const int uv_width_shift = y_width_shift >> 1;
  27. const int uv_height_shift = y_height_shift >> 1;
  28. const uint8_t ysource = y[y_height_shift * stride + y_width_shift];
  29. const uint8_t usource = u[uv_height_shift * strideuv + uv_width_shift];
  30. const uint8_t vsource = v[uv_height_shift * strideuv + uv_width_shift];
  31. if (consec_zeromv > 25 && curr_motion_magn == 0) motion = 0;
  32. return vpx_skin_pixel(ysource, usource, vsource, motion);
  33. }
  34. }
  35. void vp9_compute_skin_sb(VP9_COMP *const cpi, BLOCK_SIZE bsize, int mi_row,
  36. int mi_col) {
  37. int i, j, num_bl;
  38. VP9_COMMON *const cm = &cpi->common;
  39. const uint8_t *src_y = cpi->Source->y_buffer;
  40. const uint8_t *src_u = cpi->Source->u_buffer;
  41. const uint8_t *src_v = cpi->Source->v_buffer;
  42. const int src_ystride = cpi->Source->y_stride;
  43. const int src_uvstride = cpi->Source->uv_stride;
  44. const int y_bsize = 4 << b_width_log2_lookup[bsize];
  45. const int uv_bsize = y_bsize >> 1;
  46. const int shy = (y_bsize == 8) ? 3 : 4;
  47. const int shuv = shy - 1;
  48. const int fac = y_bsize / 8;
  49. const int y_shift = src_ystride * (mi_row << 3) + (mi_col << 3);
  50. const int uv_shift = src_uvstride * (mi_row << 2) + (mi_col << 2);
  51. const int mi_row_limit = VPXMIN(mi_row + 8, cm->mi_rows - 2);
  52. const int mi_col_limit = VPXMIN(mi_col + 8, cm->mi_cols - 2);
  53. src_y += y_shift;
  54. src_u += uv_shift;
  55. src_v += uv_shift;
  56. for (i = mi_row; i < mi_row_limit; i += fac) {
  57. num_bl = 0;
  58. for (j = mi_col; j < mi_col_limit; j += fac) {
  59. int consec_zeromv = 0;
  60. int bl_index = i * cm->mi_cols + j;
  61. int bl_index1 = bl_index + 1;
  62. int bl_index2 = bl_index + cm->mi_cols;
  63. int bl_index3 = bl_index2 + 1;
  64. // Don't detect skin on the boundary.
  65. if (i == 0 || j == 0) continue;
  66. if (bsize == BLOCK_8X8)
  67. consec_zeromv = cpi->consec_zero_mv[bl_index];
  68. else
  69. consec_zeromv = VPXMIN(cpi->consec_zero_mv[bl_index],
  70. VPXMIN(cpi->consec_zero_mv[bl_index1],
  71. VPXMIN(cpi->consec_zero_mv[bl_index2],
  72. cpi->consec_zero_mv[bl_index3])));
  73. cpi->skin_map[bl_index] =
  74. vp9_compute_skin_block(src_y, src_u, src_v, src_ystride, src_uvstride,
  75. bsize, consec_zeromv, 0);
  76. num_bl++;
  77. src_y += y_bsize;
  78. src_u += uv_bsize;
  79. src_v += uv_bsize;
  80. }
  81. src_y += (src_ystride << shy) - (num_bl << shy);
  82. src_u += (src_uvstride << shuv) - (num_bl << shuv);
  83. src_v += (src_uvstride << shuv) - (num_bl << shuv);
  84. }
  85. // Remove isolated skin blocks (none of its neighbors are skin) and isolated
  86. // non-skin blocks (all of its neighbors are skin).
  87. // Skip 4 corner blocks which have only 3 neighbors to remove isolated skin
  88. // blocks. Skip superblock borders to remove isolated non-skin blocks.
  89. for (i = mi_row; i < mi_row_limit; i += fac) {
  90. for (j = mi_col; j < mi_col_limit; j += fac) {
  91. int bl_index = i * cm->mi_cols + j;
  92. int num_neighbor = 0;
  93. int mi, mj;
  94. int non_skin_threshold = 8;
  95. // Skip 4 corners.
  96. if ((i == mi_row && (j == mi_col || j == mi_col_limit - fac)) ||
  97. (i == mi_row_limit - fac && (j == mi_col || j == mi_col_limit - fac)))
  98. continue;
  99. // There are only 5 neighbors for non-skin blocks on the border.
  100. if (i == mi_row || i == mi_row_limit - fac || j == mi_col ||
  101. j == mi_col_limit - fac)
  102. non_skin_threshold = 5;
  103. for (mi = -fac; mi <= fac; mi += fac) {
  104. for (mj = -fac; mj <= fac; mj += fac) {
  105. if (i + mi >= mi_row && i + mi < mi_row_limit && j + mj >= mi_col &&
  106. j + mj < mi_col_limit) {
  107. int bl_neighbor_index = (i + mi) * cm->mi_cols + j + mj;
  108. if (cpi->skin_map[bl_neighbor_index]) num_neighbor++;
  109. }
  110. }
  111. }
  112. if (cpi->skin_map[bl_index] && num_neighbor < 2)
  113. cpi->skin_map[bl_index] = 0;
  114. if (!cpi->skin_map[bl_index] && num_neighbor == non_skin_threshold)
  115. cpi->skin_map[bl_index] = 1;
  116. }
  117. }
  118. }
  119. #ifdef OUTPUT_YUV_SKINMAP
  120. // For viewing skin map on input source.
  121. void vp9_output_skin_map(VP9_COMP *const cpi, FILE *yuv_skinmap_file) {
  122. int i, j, mi_row, mi_col, num_bl;
  123. VP9_COMMON *const cm = &cpi->common;
  124. uint8_t *y;
  125. const uint8_t *src_y = cpi->Source->y_buffer;
  126. const int src_ystride = cpi->Source->y_stride;
  127. const int y_bsize = 16; // Use 8x8 or 16x16.
  128. const int shy = (y_bsize == 8) ? 3 : 4;
  129. const int fac = y_bsize / 8;
  130. YV12_BUFFER_CONFIG skinmap;
  131. memset(&skinmap, 0, sizeof(YV12_BUFFER_CONFIG));
  132. if (vpx_alloc_frame_buffer(&skinmap, cm->width, cm->height, cm->subsampling_x,
  133. cm->subsampling_y, VP9_ENC_BORDER_IN_PIXELS,
  134. cm->byte_alignment)) {
  135. vpx_free_frame_buffer(&skinmap);
  136. return;
  137. }
  138. memset(skinmap.buffer_alloc, 128, skinmap.frame_size);
  139. y = skinmap.y_buffer;
  140. // Loop through blocks and set skin map based on center pixel of block.
  141. // Set y to white for skin block, otherwise set to source with gray scale.
  142. // Ignore rightmost/bottom boundary blocks.
  143. for (mi_row = 0; mi_row < cm->mi_rows - 1; mi_row += fac) {
  144. num_bl = 0;
  145. for (mi_col = 0; mi_col < cm->mi_cols - 1; mi_col += fac) {
  146. const int block_index = mi_row * cm->mi_cols + mi_col;
  147. const int is_skin = cpi->skin_map[block_index];
  148. for (i = 0; i < y_bsize; i++) {
  149. for (j = 0; j < y_bsize; j++) {
  150. y[i * src_ystride + j] = is_skin ? 255 : src_y[i * src_ystride + j];
  151. }
  152. }
  153. num_bl++;
  154. y += y_bsize;
  155. src_y += y_bsize;
  156. }
  157. y += (src_ystride << shy) - (num_bl << shy);
  158. src_y += (src_ystride << shy) - (num_bl << shy);
  159. }
  160. vpx_write_yuv_frame(yuv_skinmap_file, &skinmap);
  161. vpx_free_frame_buffer(&skinmap);
  162. }
  163. #endif