From c39526da8a88facd9326ce68a6ad5ad250e096dc Mon Sep 17 00:00:00 2001 From: Jerome Jiang Date: Fri, 26 May 2017 16:20:49 -0700 Subject: [PATCH] Write skin map of vp8 skin detection for debug. Change-Id: Ica1b4e918aa759cd0ce65920f9d88452bbf9e3b4 --- vp8/common/skin_detection.c | 89 +++++++++++++++++++++++++++++++++++++ vp8/common/skin_detection.h | 12 +++++ vp8/encoder/onyx_if.c | 19 +++++++- 3 files changed, 119 insertions(+), 1 deletion(-) diff --git a/vp8/common/skin_detection.c b/vp8/common/skin_detection.c index 340e21e3a..44cbb2415 100644 --- a/vp8/common/skin_detection.c +++ b/vp8/common/skin_detection.c @@ -9,6 +9,9 @@ */ #include "vp8/common/skin_detection.h" +#include "vp8/common/alloccommon.h" +#include "vpx_dsp/vpx_dsp_common.h" +#include "vpx_mem/vpx_mem.h" #define MODEL_MODE 1 @@ -102,3 +105,89 @@ int compute_skin_block(const uint8_t *y, const uint8_t *u, const uint8_t *v, return skin_pixel(ysource, usource, vsource, motion); } } + +#ifdef OUTPUT_YUV_SKINMAP +// For viewing skin map on input source. +void compute_skin_map(VP8_COMP *const cpi, FILE *yuv_skinmap_file) { + int i, j, mb_row, mb_col, num_bl; + VP8_COMMON *const cm = &cpi->common; + uint8_t *y; + const uint8_t *src_y = cpi->Source->y_buffer; + const uint8_t *src_u = cpi->Source->u_buffer; + const uint8_t *src_v = cpi->Source->v_buffer; + const int src_ystride = cpi->Source->y_stride; + const int src_uvstride = cpi->Source->uv_stride; + + // Use center pixel or average of center 2x2 pixels. + const int mode_filter = 0; + YV12_BUFFER_CONFIG skinmap; + memset(&skinmap, 0, sizeof(skinmap)); + if (vp8_yv12_alloc_frame_buffer(&skinmap, cm->Width, cm->Height, + VP8BORDERINPIXELS) < 0) { + vpx_free_frame_buffer(&skinmap); + return; + } + memset(skinmap.buffer_alloc, 128, skinmap.frame_size); + y = skinmap.y_buffer; + // Loop through blocks and set skin map based on center pixel of block. + // Set y to white for skin block, otherwise set to source with gray scale. + // Ignore rightmost/bottom boundary blocks. + for (mb_row = 0; mb_row < cm->mb_rows - 1; mb_row += 1) { + num_bl = 0; + for (mb_col = 0; mb_col < cm->mb_cols - 1; mb_col += 1) { + int is_skin = 0; + if (mode_filter == 1) { + // Use 2x2 average at center. + uint8_t ysource = src_y[8 * src_ystride + 8]; + uint8_t usource = src_u[4 * src_uvstride + 4]; + uint8_t vsource = src_v[4 * src_uvstride + 4]; + const uint8_t ysource2 = src_y[(8 + 1) * src_ystride + 8]; + const uint8_t usource2 = src_u[(4 + 1) * src_uvstride + 4]; + const uint8_t vsource2 = src_v[(4 + 1) * src_uvstride + 4]; + const uint8_t ysource3 = src_y[8 * src_ystride + (8 + 1)]; + const uint8_t usource3 = src_u[4 * src_uvstride + (4 + 1)]; + const uint8_t vsource3 = src_v[4 * src_uvstride + (4 + 1)]; + const uint8_t ysource4 = src_y[(8 + 1) * src_ystride + (8 + 1)]; + const uint8_t usource4 = src_u[(4 + 1) * src_uvstride + (4 + 1)]; + const uint8_t vsource4 = src_v[(4 + 1) * src_uvstride + (4 + 1)]; + ysource = (ysource + ysource2 + ysource3 + ysource4) >> 2; + usource = (usource + usource2 + usource3 + usource4) >> 2; + vsource = (vsource + vsource2 + vsource3 + vsource4) >> 2; + is_skin = skin_pixel(ysource, usource, vsource, 1); + } else { + int consec_zeromv = 0; + const int bl_index = mb_row * cm->mb_cols + mb_col; + const int bl_index1 = bl_index + 1; + const int bl_index2 = bl_index + cm->mb_cols; + const int bl_index3 = bl_index2 + 1; + consec_zeromv = + VPXMIN(cpi->consec_zero_last[bl_index], + VPXMIN(cpi->consec_zero_last[bl_index1], + VPXMIN(cpi->consec_zero_last[bl_index2], + cpi->consec_zero_last[bl_index3]))); + is_skin = compute_skin_block(src_y, src_u, src_v, src_ystride, + src_uvstride, consec_zeromv, 0); + } + for (i = 0; i < 16; i++) { + for (j = 0; j < 16; j++) { + if (is_skin) + y[i * src_ystride + j] = 255; + else + y[i * src_ystride + j] = src_y[i * src_ystride + j]; + } + } + num_bl++; + y += 16; + src_y += 16; + src_u += 8; + src_v += 8; + } + y += (src_ystride << 4) - (num_bl << 4); + src_y += (src_ystride << 4) - (num_bl << 4); + src_u += (src_uvstride << 3) - (num_bl << 3); + src_v += (src_uvstride << 3) - (num_bl << 3); + } + vp8_write_yuv_frame(yuv_skinmap_file, &skinmap); + vpx_free_frame_buffer(&skinmap); +} +#endif // OUTPUT_YUV_SKINMAP diff --git a/vp8/common/skin_detection.h b/vp8/common/skin_detection.h index 131bc004a..4cb22a79a 100644 --- a/vp8/common/skin_detection.h +++ b/vp8/common/skin_detection.h @@ -11,18 +11,30 @@ #ifndef VP8_ENCODER_SKIN_DETECTION_H_ #define VP8_ENCODER_SKIN_DETECTION_H_ +#include "vp8/encoder/onyx_int.h" #include "vpx/vpx_integer.h" +#include "vpx_scale/yv12config.h" #ifdef __cplusplus extern "C" { #endif +struct VP8_COMP; + +// #define OUTPUT_YUV_SKINMAP + int skin_pixel(int y, int cb, int cr, int motion); int compute_skin_block(const uint8_t *y, const uint8_t *u, const uint8_t *v, int stride, int strideuv, int consec_zeromv, int curr_motion_magn); +#ifdef OUTPUT_YUV_SKINMAP +// For viewing skin map on input source. +void compute_skin_map(struct VP8_COMP *const cpi, FILE *yuv_skinmap_file); +extern void vp8_write_yuv_frame(FILE *f, YV12_BUFFER_CONFIG *s); +#endif + #ifdef __cplusplus } // extern "C" #endif diff --git a/vp8/encoder/onyx_if.c b/vp8/encoder/onyx_if.c index b571d29d9..fe72d7844 100644 --- a/vp8/encoder/onyx_if.c +++ b/vp8/encoder/onyx_if.c @@ -16,6 +16,7 @@ #include "vp8/common/blockd.h" #include "onyx_int.h" #include "vp8/common/systemdependent.h" +#include "vp8/common/skin_detection.h" #include "vp8/encoder/quantize.h" #include "vp8/common/alloccommon.h" #include "mcomp.h" @@ -87,6 +88,9 @@ FILE *yuv_file; #ifdef OUTPUT_YUV_DENOISED FILE *yuv_denoised_file; #endif +#ifdef OUTPUT_YUV_SKINMAP +FILE *yuv_skinmap_file = NULL; +#endif #if 0 FILE *framepsnr; @@ -1933,6 +1937,9 @@ struct VP8_COMP *vp8_create_compressor(VP8_CONFIG *oxcf) { #ifdef OUTPUT_YUV_DENOISED yuv_denoised_file = fopen("denoised.yuv", "ab"); #endif +#ifdef OUTPUT_YUV_SKINMAP + yuv_skinmap_file = fopen("skinmap.yuv", "ab"); +#endif #if 0 framepsnr = fopen("framepsnr.stt", "a"); @@ -2298,6 +2305,9 @@ void vp8_remove_compressor(VP8_COMP **ptr) { #ifdef OUTPUT_YUV_DENOISED fclose(yuv_denoised_file); #endif +#ifdef OUTPUT_YUV_SKINMAP + fclose(yuv_skinmap_file); +#endif #if 0 @@ -2474,7 +2484,8 @@ int vp8_update_entropy(VP8_COMP *cpi, int update) { return 0; } -#if defined(OUTPUT_YUV_SRC) || defined(OUTPUT_YUV_DENOISED) +#if defined(OUTPUT_YUV_SRC) || defined(OUTPUT_YUV_DENOISED) || \ + defined(OUTPUT_YUV_SKINMAP) void vp8_write_yuv_frame(FILE *yuv_file, YV12_BUFFER_CONFIG *s) { unsigned char *src = s->y_buffer; int h = s->y_height; @@ -4421,6 +4432,12 @@ static void encode_frame_to_data_rate(VP8_COMP *cpi, size_t *size, } #endif +#ifdef OUTPUT_YUV_SKINMAP + if (cpi->common.current_video_frame > 1) { + compute_skin_map(cpi, yuv_skinmap_file); + } +#endif + #if CONFIG_MULTITHREAD if (cpi->b_multi_threaded) { /* start loopfilter in separate thread */ -- 2.40.0