michael@0: /* michael@0: * Copyright (c) 2010 The WebM project authors. All Rights Reserved. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license michael@0: * that can be found in the LICENSE file in the root of the source michael@0: * tree. An additional intellectual property rights grant can be found michael@0: * in the file PATENTS. All contributing project authors may michael@0: * be found in the AUTHORS file in the root of the source tree. michael@0: */ michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "./vpx_config.h" michael@0: #include "vpx_scale/yv12config.h" michael@0: #include "vp9/common/vp9_postproc.h" michael@0: #include "vp9/common/vp9_textblit.h" michael@0: #include "vpx_scale/vpx_scale.h" michael@0: #include "vp9/common/vp9_systemdependent.h" michael@0: #include "./vp9_rtcd.h" michael@0: #include "./vpx_scale_rtcd.h" michael@0: michael@0: #define RGB_TO_YUV(t) \ michael@0: ( (0.257*(float)(t >> 16)) + (0.504*(float)(t >> 8 & 0xff)) + \ michael@0: (0.098*(float)(t & 0xff)) + 16), \ michael@0: (-(0.148*(float)(t >> 16)) - (0.291*(float)(t >> 8 & 0xff)) + \ michael@0: (0.439*(float)(t & 0xff)) + 128), \ michael@0: ( (0.439*(float)(t >> 16)) - (0.368*(float)(t >> 8 & 0xff)) - \ michael@0: (0.071*(float)(t & 0xff)) + 128) michael@0: michael@0: /* global constants */ michael@0: #if 0 && CONFIG_POSTPROC_VISUALIZER michael@0: static const unsigned char MB_PREDICTION_MODE_colors[MB_MODE_COUNT][3] = { michael@0: { RGB_TO_YUV(0x98FB98) }, /* PaleGreen */ michael@0: { RGB_TO_YUV(0x00FF00) }, /* Green */ michael@0: { RGB_TO_YUV(0xADFF2F) }, /* GreenYellow */ michael@0: { RGB_TO_YUV(0x8F0000) }, /* Dark Red */ michael@0: { RGB_TO_YUV(0x008F8F) }, /* Dark Cyan */ michael@0: { RGB_TO_YUV(0x008F8F) }, /* Dark Cyan */ michael@0: { RGB_TO_YUV(0x008F8F) }, /* Dark Cyan */ michael@0: { RGB_TO_YUV(0x8F0000) }, /* Dark Red */ michael@0: { RGB_TO_YUV(0x8F0000) }, /* Dark Red */ michael@0: { RGB_TO_YUV(0x228B22) }, /* ForestGreen */ michael@0: { RGB_TO_YUV(0x006400) }, /* DarkGreen */ michael@0: { RGB_TO_YUV(0x98F5FF) }, /* Cadet Blue */ michael@0: { RGB_TO_YUV(0x6CA6CD) }, /* Sky Blue */ michael@0: { RGB_TO_YUV(0x00008B) }, /* Dark blue */ michael@0: { RGB_TO_YUV(0x551A8B) }, /* Purple */ michael@0: { RGB_TO_YUV(0xFF0000) } /* Red */ michael@0: { RGB_TO_YUV(0xCC33FF) }, /* Magenta */ michael@0: }; michael@0: michael@0: static const unsigned char B_PREDICTION_MODE_colors[INTRA_MODES][3] = { michael@0: { RGB_TO_YUV(0x6633ff) }, /* Purple */ michael@0: { RGB_TO_YUV(0xcc33ff) }, /* Magenta */ michael@0: { RGB_TO_YUV(0xff33cc) }, /* Pink */ michael@0: { RGB_TO_YUV(0xff3366) }, /* Coral */ michael@0: { RGB_TO_YUV(0x3366ff) }, /* Blue */ michael@0: { RGB_TO_YUV(0xed00f5) }, /* Dark Blue */ michael@0: { RGB_TO_YUV(0x2e00b8) }, /* Dark Purple */ michael@0: { RGB_TO_YUV(0xff6633) }, /* Orange */ michael@0: { RGB_TO_YUV(0x33ccff) }, /* Light Blue */ michael@0: { RGB_TO_YUV(0x8ab800) }, /* Green */ michael@0: { RGB_TO_YUV(0xffcc33) }, /* Light Orange */ michael@0: { RGB_TO_YUV(0x33ffcc) }, /* Aqua */ michael@0: { RGB_TO_YUV(0x66ff33) }, /* Light Green */ michael@0: { RGB_TO_YUV(0xccff33) }, /* Yellow */ michael@0: }; michael@0: michael@0: static const unsigned char MV_REFERENCE_FRAME_colors[MAX_REF_FRAMES][3] = { michael@0: { RGB_TO_YUV(0x00ff00) }, /* Blue */ michael@0: { RGB_TO_YUV(0x0000ff) }, /* Green */ michael@0: { RGB_TO_YUV(0xffff00) }, /* Yellow */ michael@0: { RGB_TO_YUV(0xff0000) }, /* Red */ michael@0: }; michael@0: #endif michael@0: michael@0: static const short kernel5[] = { michael@0: 1, 1, 4, 1, 1 michael@0: }; michael@0: michael@0: const short vp9_rv[] = { michael@0: 8, 5, 2, 2, 8, 12, 4, 9, 8, 3, michael@0: 0, 3, 9, 0, 0, 0, 8, 3, 14, 4, michael@0: 10, 1, 11, 14, 1, 14, 9, 6, 12, 11, michael@0: 8, 6, 10, 0, 0, 8, 9, 0, 3, 14, michael@0: 8, 11, 13, 4, 2, 9, 0, 3, 9, 6, michael@0: 1, 2, 3, 14, 13, 1, 8, 2, 9, 7, michael@0: 3, 3, 1, 13, 13, 6, 6, 5, 2, 7, michael@0: 11, 9, 11, 8, 7, 3, 2, 0, 13, 13, michael@0: 14, 4, 12, 5, 12, 10, 8, 10, 13, 10, michael@0: 4, 14, 4, 10, 0, 8, 11, 1, 13, 7, michael@0: 7, 14, 6, 14, 13, 2, 13, 5, 4, 4, michael@0: 0, 10, 0, 5, 13, 2, 12, 7, 11, 13, michael@0: 8, 0, 4, 10, 7, 2, 7, 2, 2, 5, michael@0: 3, 4, 7, 3, 3, 14, 14, 5, 9, 13, michael@0: 3, 14, 3, 6, 3, 0, 11, 8, 13, 1, michael@0: 13, 1, 12, 0, 10, 9, 7, 6, 2, 8, michael@0: 5, 2, 13, 7, 1, 13, 14, 7, 6, 7, michael@0: 9, 6, 10, 11, 7, 8, 7, 5, 14, 8, michael@0: 4, 4, 0, 8, 7, 10, 0, 8, 14, 11, michael@0: 3, 12, 5, 7, 14, 3, 14, 5, 2, 6, michael@0: 11, 12, 12, 8, 0, 11, 13, 1, 2, 0, michael@0: 5, 10, 14, 7, 8, 0, 4, 11, 0, 8, michael@0: 0, 3, 10, 5, 8, 0, 11, 6, 7, 8, michael@0: 10, 7, 13, 9, 2, 5, 1, 5, 10, 2, michael@0: 4, 3, 5, 6, 10, 8, 9, 4, 11, 14, michael@0: 0, 10, 0, 5, 13, 2, 12, 7, 11, 13, michael@0: 8, 0, 4, 10, 7, 2, 7, 2, 2, 5, michael@0: 3, 4, 7, 3, 3, 14, 14, 5, 9, 13, michael@0: 3, 14, 3, 6, 3, 0, 11, 8, 13, 1, michael@0: 13, 1, 12, 0, 10, 9, 7, 6, 2, 8, michael@0: 5, 2, 13, 7, 1, 13, 14, 7, 6, 7, michael@0: 9, 6, 10, 11, 7, 8, 7, 5, 14, 8, michael@0: 4, 4, 0, 8, 7, 10, 0, 8, 14, 11, michael@0: 3, 12, 5, 7, 14, 3, 14, 5, 2, 6, michael@0: 11, 12, 12, 8, 0, 11, 13, 1, 2, 0, michael@0: 5, 10, 14, 7, 8, 0, 4, 11, 0, 8, michael@0: 0, 3, 10, 5, 8, 0, 11, 6, 7, 8, michael@0: 10, 7, 13, 9, 2, 5, 1, 5, 10, 2, michael@0: 4, 3, 5, 6, 10, 8, 9, 4, 11, 14, michael@0: 3, 8, 3, 7, 8, 5, 11, 4, 12, 3, michael@0: 11, 9, 14, 8, 14, 13, 4, 3, 1, 2, michael@0: 14, 6, 5, 4, 4, 11, 4, 6, 2, 1, michael@0: 5, 8, 8, 12, 13, 5, 14, 10, 12, 13, michael@0: 0, 9, 5, 5, 11, 10, 13, 9, 10, 13, michael@0: }; michael@0: michael@0: michael@0: /**************************************************************************** michael@0: */ michael@0: void vp9_post_proc_down_and_across_c(const uint8_t *src_ptr, michael@0: uint8_t *dst_ptr, michael@0: int src_pixels_per_line, michael@0: int dst_pixels_per_line, michael@0: int rows, michael@0: int cols, michael@0: int flimit) { michael@0: uint8_t const *p_src; michael@0: uint8_t *p_dst; michael@0: int row; michael@0: int col; michael@0: int i; michael@0: int v; michael@0: int pitch = src_pixels_per_line; michael@0: uint8_t d[8]; michael@0: (void)dst_pixels_per_line; michael@0: michael@0: for (row = 0; row < rows; row++) { michael@0: /* post_proc_down for one row */ michael@0: p_src = src_ptr; michael@0: p_dst = dst_ptr; michael@0: michael@0: for (col = 0; col < cols; col++) { michael@0: int kernel = 4; michael@0: int v = p_src[col]; michael@0: michael@0: for (i = -2; i <= 2; i++) { michael@0: if (abs(v - p_src[col + i * pitch]) > flimit) michael@0: goto down_skip_convolve; michael@0: michael@0: kernel += kernel5[2 + i] * p_src[col + i * pitch]; michael@0: } michael@0: michael@0: v = (kernel >> 3); michael@0: down_skip_convolve: michael@0: p_dst[col] = v; michael@0: } michael@0: michael@0: /* now post_proc_across */ michael@0: p_src = dst_ptr; michael@0: p_dst = dst_ptr; michael@0: michael@0: for (i = 0; i < 8; i++) michael@0: d[i] = p_src[i]; michael@0: michael@0: for (col = 0; col < cols; col++) { michael@0: int kernel = 4; michael@0: v = p_src[col]; michael@0: michael@0: d[col & 7] = v; michael@0: michael@0: for (i = -2; i <= 2; i++) { michael@0: if (abs(v - p_src[col + i]) > flimit) michael@0: goto across_skip_convolve; michael@0: michael@0: kernel += kernel5[2 + i] * p_src[col + i]; michael@0: } michael@0: michael@0: d[col & 7] = (kernel >> 3); michael@0: across_skip_convolve: michael@0: michael@0: if (col >= 2) michael@0: p_dst[col - 2] = d[(col - 2) & 7]; michael@0: } michael@0: michael@0: /* handle the last two pixels */ michael@0: p_dst[col - 2] = d[(col - 2) & 7]; michael@0: p_dst[col - 1] = d[(col - 1) & 7]; michael@0: michael@0: michael@0: /* next row */ michael@0: src_ptr += pitch; michael@0: dst_ptr += pitch; michael@0: } michael@0: } michael@0: michael@0: static int q2mbl(int x) { michael@0: if (x < 20) x = 20; michael@0: michael@0: x = 50 + (x - 50) * 10 / 8; michael@0: return x * x / 3; michael@0: } michael@0: michael@0: void vp9_mbpost_proc_across_ip_c(uint8_t *src, int pitch, michael@0: int rows, int cols, int flimit) { michael@0: int r, c, i; michael@0: michael@0: uint8_t *s = src; michael@0: uint8_t d[16]; michael@0: michael@0: michael@0: for (r = 0; r < rows; r++) { michael@0: int sumsq = 0; michael@0: int sum = 0; michael@0: michael@0: for (i = -8; i <= 6; i++) { michael@0: sumsq += s[i] * s[i]; michael@0: sum += s[i]; michael@0: d[i + 8] = 0; michael@0: } michael@0: michael@0: for (c = 0; c < cols + 8; c++) { michael@0: int x = s[c + 7] - s[c - 8]; michael@0: int y = s[c + 7] + s[c - 8]; michael@0: michael@0: sum += x; michael@0: sumsq += x * y; michael@0: michael@0: d[c & 15] = s[c]; michael@0: michael@0: if (sumsq * 15 - sum * sum < flimit) { michael@0: d[c & 15] = (8 + sum + s[c]) >> 4; michael@0: } michael@0: michael@0: s[c - 8] = d[(c - 8) & 15]; michael@0: } michael@0: michael@0: s += pitch; michael@0: } michael@0: } michael@0: michael@0: void vp9_mbpost_proc_down_c(uint8_t *dst, int pitch, michael@0: int rows, int cols, int flimit) { michael@0: int r, c, i; michael@0: const short *rv3 = &vp9_rv[63 & rand()]; // NOLINT michael@0: michael@0: for (c = 0; c < cols; c++) { michael@0: uint8_t *s = &dst[c]; michael@0: int sumsq = 0; michael@0: int sum = 0; michael@0: uint8_t d[16]; michael@0: const short *rv2 = rv3 + ((c * 17) & 127); michael@0: michael@0: for (i = -8; i <= 6; i++) { michael@0: sumsq += s[i * pitch] * s[i * pitch]; michael@0: sum += s[i * pitch]; michael@0: } michael@0: michael@0: for (r = 0; r < rows + 8; r++) { michael@0: sumsq += s[7 * pitch] * s[ 7 * pitch] - s[-8 * pitch] * s[-8 * pitch]; michael@0: sum += s[7 * pitch] - s[-8 * pitch]; michael@0: d[r & 15] = s[0]; michael@0: michael@0: if (sumsq * 15 - sum * sum < flimit) { michael@0: d[r & 15] = (rv2[r & 127] + sum + s[0]) >> 4; michael@0: } michael@0: michael@0: s[-8 * pitch] = d[(r - 8) & 15]; michael@0: s += pitch; michael@0: } michael@0: } michael@0: } michael@0: michael@0: static void deblock_and_de_macro_block(YV12_BUFFER_CONFIG *source, michael@0: YV12_BUFFER_CONFIG *post, michael@0: int q, michael@0: int low_var_thresh, michael@0: int flag) { michael@0: double level = 6.0e-05 * q * q * q - .0067 * q * q + .306 * q + .0065; michael@0: int ppl = (int)(level + .5); michael@0: (void) low_var_thresh; michael@0: (void) flag; michael@0: michael@0: vp9_post_proc_down_and_across(source->y_buffer, post->y_buffer, michael@0: source->y_stride, post->y_stride, michael@0: source->y_height, source->y_width, ppl); michael@0: michael@0: vp9_mbpost_proc_across_ip(post->y_buffer, post->y_stride, post->y_height, michael@0: post->y_width, q2mbl(q)); michael@0: michael@0: vp9_mbpost_proc_down(post->y_buffer, post->y_stride, post->y_height, michael@0: post->y_width, q2mbl(q)); michael@0: michael@0: vp9_post_proc_down_and_across(source->u_buffer, post->u_buffer, michael@0: source->uv_stride, post->uv_stride, michael@0: source->uv_height, source->uv_width, ppl); michael@0: vp9_post_proc_down_and_across(source->v_buffer, post->v_buffer, michael@0: source->uv_stride, post->uv_stride, michael@0: source->uv_height, source->uv_width, ppl); michael@0: } michael@0: michael@0: void vp9_deblock(const YV12_BUFFER_CONFIG *src, YV12_BUFFER_CONFIG *dst, michael@0: int q) { michael@0: const int ppl = (int)(6.0e-05 * q * q * q - 0.0067 * q * q + 0.306 * q michael@0: + 0.0065 + 0.5); michael@0: int i; michael@0: michael@0: const uint8_t *const srcs[4] = {src->y_buffer, src->u_buffer, src->v_buffer, michael@0: src->alpha_buffer}; michael@0: const int src_strides[4] = {src->y_stride, src->uv_stride, src->uv_stride, michael@0: src->alpha_stride}; michael@0: const int src_widths[4] = {src->y_width, src->uv_width, src->uv_width, michael@0: src->alpha_width}; michael@0: const int src_heights[4] = {src->y_height, src->uv_height, src->uv_height, michael@0: src->alpha_height}; michael@0: michael@0: uint8_t *const dsts[4] = {dst->y_buffer, dst->u_buffer, dst->v_buffer, michael@0: dst->alpha_buffer}; michael@0: const int dst_strides[4] = {dst->y_stride, dst->uv_stride, dst->uv_stride, michael@0: dst->alpha_stride}; michael@0: michael@0: for (i = 0; i < MAX_MB_PLANE; ++i) michael@0: vp9_post_proc_down_and_across(srcs[i], dsts[i], michael@0: src_strides[i], dst_strides[i], michael@0: src_heights[i], src_widths[i], ppl); michael@0: } michael@0: michael@0: void vp9_denoise(const YV12_BUFFER_CONFIG *src, YV12_BUFFER_CONFIG *dst, michael@0: int q) { michael@0: const int ppl = (int)(6.0e-05 * q * q * q - 0.0067 * q * q + 0.306 * q michael@0: + 0.0065 + 0.5); michael@0: int i; michael@0: michael@0: const uint8_t *const srcs[4] = {src->y_buffer, src->u_buffer, src->v_buffer, michael@0: src->alpha_buffer}; michael@0: const int src_strides[4] = {src->y_stride, src->uv_stride, src->uv_stride, michael@0: src->alpha_stride}; michael@0: const int src_widths[4] = {src->y_width, src->uv_width, src->uv_width, michael@0: src->alpha_width}; michael@0: const int src_heights[4] = {src->y_height, src->uv_height, src->uv_height, michael@0: src->alpha_height}; michael@0: michael@0: uint8_t *const dsts[4] = {dst->y_buffer, dst->u_buffer, dst->v_buffer, michael@0: dst->alpha_buffer}; michael@0: const int dst_strides[4] = {dst->y_stride, dst->uv_stride, dst->uv_stride, michael@0: dst->alpha_stride}; michael@0: michael@0: for (i = 0; i < MAX_MB_PLANE; ++i) { michael@0: const int src_stride = src_strides[i]; michael@0: const uint8_t *const src = srcs[i] + 2 * src_stride + 2; michael@0: const int src_width = src_widths[i] - 4; michael@0: const int src_height = src_heights[i] - 4; michael@0: michael@0: const int dst_stride = dst_strides[i]; michael@0: uint8_t *const dst = dsts[i] + 2 * dst_stride + 2; michael@0: michael@0: vp9_post_proc_down_and_across(src, dst, src_stride, dst_stride, michael@0: src_height, src_width, ppl); michael@0: } michael@0: } michael@0: michael@0: double vp9_gaussian(double sigma, double mu, double x) { michael@0: return 1 / (sigma * sqrt(2.0 * 3.14159265)) * michael@0: (exp(-(x - mu) * (x - mu) / (2 * sigma * sigma))); michael@0: } michael@0: michael@0: static void fillrd(struct postproc_state *state, int q, int a) { michael@0: char char_dist[300]; michael@0: michael@0: double sigma; michael@0: int ai = a, qi = q, i; michael@0: michael@0: vp9_clear_system_state(); michael@0: michael@0: sigma = ai + .5 + .6 * (63 - qi) / 63.0; michael@0: michael@0: /* set up a lookup table of 256 entries that matches michael@0: * a gaussian distribution with sigma determined by q. michael@0: */ michael@0: { michael@0: double i; michael@0: int next, j; michael@0: michael@0: next = 0; michael@0: michael@0: for (i = -32; i < 32; i++) { michael@0: int a = (int)(.5 + 256 * vp9_gaussian(sigma, 0, i)); michael@0: michael@0: if (a) { michael@0: for (j = 0; j < a; j++) { michael@0: char_dist[next + j] = (char) i; michael@0: } michael@0: michael@0: next = next + j; michael@0: } michael@0: } michael@0: michael@0: for (; next < 256; next++) michael@0: char_dist[next] = 0; michael@0: } michael@0: michael@0: for (i = 0; i < 3072; i++) { michael@0: state->noise[i] = char_dist[rand() & 0xff]; // NOLINT michael@0: } michael@0: michael@0: for (i = 0; i < 16; i++) { michael@0: state->blackclamp[i] = -char_dist[0]; michael@0: state->whiteclamp[i] = -char_dist[0]; michael@0: state->bothclamp[i] = -2 * char_dist[0]; michael@0: } michael@0: michael@0: state->last_q = q; michael@0: state->last_noise = a; michael@0: } michael@0: michael@0: /**************************************************************************** michael@0: * michael@0: * ROUTINE : plane_add_noise_c michael@0: * michael@0: * INPUTS : unsigned char *Start starting address of buffer to michael@0: * add gaussian noise to michael@0: * unsigned int width width of plane michael@0: * unsigned int height height of plane michael@0: * int pitch distance between subsequent lines of frame michael@0: * int q quantizer used to determine amount of noise michael@0: * to add michael@0: * michael@0: * OUTPUTS : None. michael@0: * michael@0: * RETURNS : void. michael@0: * michael@0: * FUNCTION : adds gaussian noise to a plane of pixels michael@0: * michael@0: * SPECIAL NOTES : None. michael@0: * michael@0: ****************************************************************************/ michael@0: void vp9_plane_add_noise_c(uint8_t *start, char *noise, michael@0: char blackclamp[16], michael@0: char whiteclamp[16], michael@0: char bothclamp[16], michael@0: unsigned int width, unsigned int height, int pitch) { michael@0: unsigned int i, j; michael@0: michael@0: for (i = 0; i < height; i++) { michael@0: uint8_t *pos = start + i * pitch; michael@0: char *ref = (char *)(noise + (rand() & 0xff)); // NOLINT michael@0: michael@0: for (j = 0; j < width; j++) { michael@0: if (pos[j] < blackclamp[0]) michael@0: pos[j] = blackclamp[0]; michael@0: michael@0: if (pos[j] > 255 + whiteclamp[0]) michael@0: pos[j] = 255 + whiteclamp[0]; michael@0: michael@0: pos[j] += ref[j]; michael@0: } michael@0: } michael@0: } michael@0: michael@0: /* Blend the macro block with a solid colored square. Leave the michael@0: * edges unblended to give distinction to macro blocks in areas michael@0: * filled with the same color block. michael@0: */ michael@0: void vp9_blend_mb_inner_c(uint8_t *y, uint8_t *u, uint8_t *v, michael@0: int y1, int u1, int v1, int alpha, int stride) { michael@0: int i, j; michael@0: int y1_const = y1 * ((1 << 16) - alpha); michael@0: int u1_const = u1 * ((1 << 16) - alpha); michael@0: int v1_const = v1 * ((1 << 16) - alpha); michael@0: michael@0: y += 2 * stride + 2; michael@0: for (i = 0; i < 12; i++) { michael@0: for (j = 0; j < 12; j++) { michael@0: y[j] = (y[j] * alpha + y1_const) >> 16; michael@0: } michael@0: y += stride; michael@0: } michael@0: michael@0: stride >>= 1; michael@0: michael@0: u += stride + 1; michael@0: v += stride + 1; michael@0: michael@0: for (i = 0; i < 6; i++) { michael@0: for (j = 0; j < 6; j++) { michael@0: u[j] = (u[j] * alpha + u1_const) >> 16; michael@0: v[j] = (v[j] * alpha + v1_const) >> 16; michael@0: } michael@0: u += stride; michael@0: v += stride; michael@0: } michael@0: } michael@0: michael@0: /* Blend only the edge of the macro block. Leave center michael@0: * unblended to allow for other visualizations to be layered. michael@0: */ michael@0: void vp9_blend_mb_outer_c(uint8_t *y, uint8_t *u, uint8_t *v, michael@0: int y1, int u1, int v1, int alpha, int stride) { michael@0: int i, j; michael@0: int y1_const = y1 * ((1 << 16) - alpha); michael@0: int u1_const = u1 * ((1 << 16) - alpha); michael@0: int v1_const = v1 * ((1 << 16) - alpha); michael@0: michael@0: for (i = 0; i < 2; i++) { michael@0: for (j = 0; j < 16; j++) { michael@0: y[j] = (y[j] * alpha + y1_const) >> 16; michael@0: } michael@0: y += stride; michael@0: } michael@0: michael@0: for (i = 0; i < 12; i++) { michael@0: y[0] = (y[0] * alpha + y1_const) >> 16; michael@0: y[1] = (y[1] * alpha + y1_const) >> 16; michael@0: y[14] = (y[14] * alpha + y1_const) >> 16; michael@0: y[15] = (y[15] * alpha + y1_const) >> 16; michael@0: y += stride; michael@0: } michael@0: michael@0: for (i = 0; i < 2; i++) { michael@0: for (j = 0; j < 16; j++) { michael@0: y[j] = (y[j] * alpha + y1_const) >> 16; michael@0: } michael@0: y += stride; michael@0: } michael@0: michael@0: stride >>= 1; michael@0: michael@0: for (j = 0; j < 8; j++) { michael@0: u[j] = (u[j] * alpha + u1_const) >> 16; michael@0: v[j] = (v[j] * alpha + v1_const) >> 16; michael@0: } michael@0: u += stride; michael@0: v += stride; michael@0: michael@0: for (i = 0; i < 6; i++) { michael@0: u[0] = (u[0] * alpha + u1_const) >> 16; michael@0: v[0] = (v[0] * alpha + v1_const) >> 16; michael@0: michael@0: u[7] = (u[7] * alpha + u1_const) >> 16; michael@0: v[7] = (v[7] * alpha + v1_const) >> 16; michael@0: michael@0: u += stride; michael@0: v += stride; michael@0: } michael@0: michael@0: for (j = 0; j < 8; j++) { michael@0: u[j] = (u[j] * alpha + u1_const) >> 16; michael@0: v[j] = (v[j] * alpha + v1_const) >> 16; michael@0: } michael@0: } michael@0: michael@0: void vp9_blend_b_c(uint8_t *y, uint8_t *u, uint8_t *v, michael@0: int y1, int u1, int v1, int alpha, int stride) { michael@0: int i, j; michael@0: int y1_const = y1 * ((1 << 16) - alpha); michael@0: int u1_const = u1 * ((1 << 16) - alpha); michael@0: int v1_const = v1 * ((1 << 16) - alpha); michael@0: michael@0: for (i = 0; i < 4; i++) { michael@0: for (j = 0; j < 4; j++) { michael@0: y[j] = (y[j] * alpha + y1_const) >> 16; michael@0: } michael@0: y += stride; michael@0: } michael@0: michael@0: stride >>= 1; michael@0: michael@0: for (i = 0; i < 2; i++) { michael@0: for (j = 0; j < 2; j++) { michael@0: u[j] = (u[j] * alpha + u1_const) >> 16; michael@0: v[j] = (v[j] * alpha + v1_const) >> 16; michael@0: } michael@0: u += stride; michael@0: v += stride; michael@0: } michael@0: } michael@0: michael@0: static void constrain_line(int x0, int *x1, int y0, int *y1, michael@0: int width, int height) { michael@0: int dx; michael@0: int dy; michael@0: michael@0: if (*x1 > width) { michael@0: dx = *x1 - x0; michael@0: dy = *y1 - y0; michael@0: michael@0: *x1 = width; michael@0: if (dx) michael@0: *y1 = ((width - x0) * dy) / dx + y0; michael@0: } michael@0: if (*x1 < 0) { michael@0: dx = *x1 - x0; michael@0: dy = *y1 - y0; michael@0: michael@0: *x1 = 0; michael@0: if (dx) michael@0: *y1 = ((0 - x0) * dy) / dx + y0; michael@0: } michael@0: if (*y1 > height) { michael@0: dx = *x1 - x0; michael@0: dy = *y1 - y0; michael@0: michael@0: *y1 = height; michael@0: if (dy) michael@0: *x1 = ((height - y0) * dx) / dy + x0; michael@0: } michael@0: if (*y1 < 0) { michael@0: dx = *x1 - x0; michael@0: dy = *y1 - y0; michael@0: michael@0: *y1 = 0; michael@0: if (dy) michael@0: *x1 = ((0 - y0) * dx) / dy + x0; michael@0: } michael@0: } michael@0: michael@0: int vp9_post_proc_frame(struct VP9Common *cm, michael@0: YV12_BUFFER_CONFIG *dest, vp9_ppflags_t *ppflags) { michael@0: int q = cm->lf.filter_level * 10 / 6; michael@0: int flags = ppflags->post_proc_flag; michael@0: int deblock_level = ppflags->deblocking_level; michael@0: int noise_level = ppflags->noise_level; michael@0: michael@0: if (!cm->frame_to_show) michael@0: return -1; michael@0: michael@0: if (q > 63) michael@0: q = 63; michael@0: michael@0: if (!flags) { michael@0: *dest = *cm->frame_to_show; michael@0: return 0; michael@0: } michael@0: michael@0: #if ARCH_X86||ARCH_X86_64 michael@0: vpx_reset_mmx_state(); michael@0: #endif michael@0: michael@0: if (flags & VP9D_DEMACROBLOCK) { michael@0: deblock_and_de_macro_block(cm->frame_to_show, &cm->post_proc_buffer, michael@0: q + (deblock_level - 5) * 10, 1, 0); michael@0: } else if (flags & VP9D_DEBLOCK) { michael@0: vp9_deblock(cm->frame_to_show, &cm->post_proc_buffer, q); michael@0: } else { michael@0: vp8_yv12_copy_frame(cm->frame_to_show, &cm->post_proc_buffer); michael@0: } michael@0: michael@0: if (flags & VP9D_ADDNOISE) { michael@0: if (cm->postproc_state.last_q != q michael@0: || cm->postproc_state.last_noise != noise_level) { michael@0: fillrd(&cm->postproc_state, 63 - q, noise_level); michael@0: } michael@0: michael@0: vp9_plane_add_noise(cm->post_proc_buffer.y_buffer, michael@0: cm->postproc_state.noise, michael@0: cm->postproc_state.blackclamp, michael@0: cm->postproc_state.whiteclamp, michael@0: cm->postproc_state.bothclamp, michael@0: cm->post_proc_buffer.y_width, michael@0: cm->post_proc_buffer.y_height, michael@0: cm->post_proc_buffer.y_stride); michael@0: } michael@0: michael@0: #if 0 && CONFIG_POSTPROC_VISUALIZER michael@0: if (flags & VP9D_DEBUG_TXT_FRAME_INFO) { michael@0: char message[512]; michael@0: snprintf(message, sizeof(message) -1, michael@0: "F%1dG%1dQ%3dF%3dP%d_s%dx%d", michael@0: (cm->frame_type == KEY_FRAME), michael@0: cm->refresh_golden_frame, michael@0: cm->base_qindex, michael@0: cm->filter_level, michael@0: flags, michael@0: cm->mb_cols, cm->mb_rows); michael@0: vp9_blit_text(message, cm->post_proc_buffer.y_buffer, michael@0: cm->post_proc_buffer.y_stride); michael@0: } michael@0: michael@0: if (flags & VP9D_DEBUG_TXT_MBLK_MODES) { michael@0: int i, j; michael@0: uint8_t *y_ptr; michael@0: YV12_BUFFER_CONFIG *post = &cm->post_proc_buffer; michael@0: int mb_rows = post->y_height >> 4; michael@0: int mb_cols = post->y_width >> 4; michael@0: int mb_index = 0; michael@0: MODE_INFO *mi = cm->mi; michael@0: michael@0: y_ptr = post->y_buffer + 4 * post->y_stride + 4; michael@0: michael@0: /* vp9_filter each macro block */ michael@0: for (i = 0; i < mb_rows; i++) { michael@0: for (j = 0; j < mb_cols; j++) { michael@0: char zz[4]; michael@0: michael@0: snprintf(zz, sizeof(zz) - 1, "%c", mi[mb_index].mbmi.mode + 'a'); michael@0: michael@0: vp9_blit_text(zz, y_ptr, post->y_stride); michael@0: mb_index++; michael@0: y_ptr += 16; michael@0: } michael@0: michael@0: mb_index++; /* border */ michael@0: y_ptr += post->y_stride * 16 - post->y_width; michael@0: } michael@0: } michael@0: michael@0: if (flags & VP9D_DEBUG_TXT_DC_DIFF) { michael@0: int i, j; michael@0: uint8_t *y_ptr; michael@0: YV12_BUFFER_CONFIG *post = &cm->post_proc_buffer; michael@0: int mb_rows = post->y_height >> 4; michael@0: int mb_cols = post->y_width >> 4; michael@0: int mb_index = 0; michael@0: MODE_INFO *mi = cm->mi; michael@0: michael@0: y_ptr = post->y_buffer + 4 * post->y_stride + 4; michael@0: michael@0: /* vp9_filter each macro block */ michael@0: for (i = 0; i < mb_rows; i++) { michael@0: for (j = 0; j < mb_cols; j++) { michael@0: char zz[4]; michael@0: int dc_diff = !(mi[mb_index].mbmi.mode != I4X4_PRED && michael@0: mi[mb_index].mbmi.mode != SPLITMV && michael@0: mi[mb_index].mbmi.skip_coeff); michael@0: michael@0: if (cm->frame_type == KEY_FRAME) michael@0: snprintf(zz, sizeof(zz) - 1, "a"); michael@0: else michael@0: snprintf(zz, sizeof(zz) - 1, "%c", dc_diff + '0'); michael@0: michael@0: vp9_blit_text(zz, y_ptr, post->y_stride); michael@0: mb_index++; michael@0: y_ptr += 16; michael@0: } michael@0: michael@0: mb_index++; /* border */ michael@0: y_ptr += post->y_stride * 16 - post->y_width; michael@0: } michael@0: } michael@0: michael@0: if (flags & VP9D_DEBUG_TXT_RATE_INFO) { michael@0: char message[512]; michael@0: snprintf(message, sizeof(message), michael@0: "Bitrate: %10.2f framerate: %10.2f ", michael@0: cm->bitrate, cm->framerate); michael@0: vp9_blit_text(message, cm->post_proc_buffer.y_buffer, michael@0: cm->post_proc_buffer.y_stride); michael@0: } michael@0: michael@0: /* Draw motion vectors */ michael@0: if ((flags & VP9D_DEBUG_DRAW_MV) && ppflags->display_mv_flag) { michael@0: YV12_BUFFER_CONFIG *post = &cm->post_proc_buffer; michael@0: int width = post->y_width; michael@0: int height = post->y_height; michael@0: uint8_t *y_buffer = cm->post_proc_buffer.y_buffer; michael@0: int y_stride = cm->post_proc_buffer.y_stride; michael@0: MODE_INFO *mi = cm->mi; michael@0: int x0, y0; michael@0: michael@0: for (y0 = 0; y0 < height; y0 += 16) { michael@0: for (x0 = 0; x0 < width; x0 += 16) { michael@0: int x1, y1; michael@0: michael@0: if (!(ppflags->display_mv_flag & (1 << mi->mbmi.mode))) { michael@0: mi++; michael@0: continue; michael@0: } michael@0: michael@0: if (mi->mbmi.mode == SPLITMV) { michael@0: switch (mi->mbmi.partitioning) { michael@0: case PARTITIONING_16X8 : { /* mv_top_bottom */ michael@0: union b_mode_info *bmi = &mi->bmi[0]; michael@0: MV *mv = &bmi->mv.as_mv; michael@0: michael@0: x1 = x0 + 8 + (mv->col >> 3); michael@0: y1 = y0 + 4 + (mv->row >> 3); michael@0: michael@0: constrain_line(x0 + 8, &x1, y0 + 4, &y1, width, height); michael@0: vp9_blit_line(x0 + 8, x1, y0 + 4, y1, y_buffer, y_stride); michael@0: michael@0: bmi = &mi->bmi[8]; michael@0: michael@0: x1 = x0 + 8 + (mv->col >> 3); michael@0: y1 = y0 + 12 + (mv->row >> 3); michael@0: michael@0: constrain_line(x0 + 8, &x1, y0 + 12, &y1, width, height); michael@0: vp9_blit_line(x0 + 8, x1, y0 + 12, y1, y_buffer, y_stride); michael@0: michael@0: break; michael@0: } michael@0: case PARTITIONING_8X16 : { /* mv_left_right */ michael@0: union b_mode_info *bmi = &mi->bmi[0]; michael@0: MV *mv = &bmi->mv.as_mv; michael@0: michael@0: x1 = x0 + 4 + (mv->col >> 3); michael@0: y1 = y0 + 8 + (mv->row >> 3); michael@0: michael@0: constrain_line(x0 + 4, &x1, y0 + 8, &y1, width, height); michael@0: vp9_blit_line(x0 + 4, x1, y0 + 8, y1, y_buffer, y_stride); michael@0: michael@0: bmi = &mi->bmi[2]; michael@0: michael@0: x1 = x0 + 12 + (mv->col >> 3); michael@0: y1 = y0 + 8 + (mv->row >> 3); michael@0: michael@0: constrain_line(x0 + 12, &x1, y0 + 8, &y1, width, height); michael@0: vp9_blit_line(x0 + 12, x1, y0 + 8, y1, y_buffer, y_stride); michael@0: michael@0: break; michael@0: } michael@0: case PARTITIONING_8X8 : { /* mv_quarters */ michael@0: union b_mode_info *bmi = &mi->bmi[0]; michael@0: MV *mv = &bmi->mv.as_mv; michael@0: michael@0: x1 = x0 + 4 + (mv->col >> 3); michael@0: y1 = y0 + 4 + (mv->row >> 3); michael@0: michael@0: constrain_line(x0 + 4, &x1, y0 + 4, &y1, width, height); michael@0: vp9_blit_line(x0 + 4, x1, y0 + 4, y1, y_buffer, y_stride); michael@0: michael@0: bmi = &mi->bmi[2]; michael@0: michael@0: x1 = x0 + 12 + (mv->col >> 3); michael@0: y1 = y0 + 4 + (mv->row >> 3); michael@0: michael@0: constrain_line(x0 + 12, &x1, y0 + 4, &y1, width, height); michael@0: vp9_blit_line(x0 + 12, x1, y0 + 4, y1, y_buffer, y_stride); michael@0: michael@0: bmi = &mi->bmi[8]; michael@0: michael@0: x1 = x0 + 4 + (mv->col >> 3); michael@0: y1 = y0 + 12 + (mv->row >> 3); michael@0: michael@0: constrain_line(x0 + 4, &x1, y0 + 12, &y1, width, height); michael@0: vp9_blit_line(x0 + 4, x1, y0 + 12, y1, y_buffer, y_stride); michael@0: michael@0: bmi = &mi->bmi[10]; michael@0: michael@0: x1 = x0 + 12 + (mv->col >> 3); michael@0: y1 = y0 + 12 + (mv->row >> 3); michael@0: michael@0: constrain_line(x0 + 12, &x1, y0 + 12, &y1, width, height); michael@0: vp9_blit_line(x0 + 12, x1, y0 + 12, y1, y_buffer, y_stride); michael@0: break; michael@0: } michael@0: case PARTITIONING_4X4: michael@0: default : { michael@0: union b_mode_info *bmi = mi->bmi; michael@0: int bx0, by0; michael@0: michael@0: for (by0 = y0; by0 < (y0 + 16); by0 += 4) { michael@0: for (bx0 = x0; bx0 < (x0 + 16); bx0 += 4) { michael@0: MV *mv = &bmi->mv.as_mv; michael@0: michael@0: x1 = bx0 + 2 + (mv->col >> 3); michael@0: y1 = by0 + 2 + (mv->row >> 3); michael@0: michael@0: constrain_line(bx0 + 2, &x1, by0 + 2, &y1, width, height); michael@0: vp9_blit_line(bx0 + 2, x1, by0 + 2, y1, y_buffer, y_stride); michael@0: michael@0: bmi++; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } else if (is_inter_mode(mi->mbmi.mode)) { michael@0: MV *mv = &mi->mbmi.mv.as_mv; michael@0: const int lx0 = x0 + 8; michael@0: const int ly0 = y0 + 8; michael@0: michael@0: x1 = lx0 + (mv->col >> 3); michael@0: y1 = ly0 + (mv->row >> 3); michael@0: michael@0: if (x1 != lx0 && y1 != ly0) { michael@0: constrain_line(lx0, &x1, ly0 - 1, &y1, width, height); michael@0: vp9_blit_line(lx0, x1, ly0 - 1, y1, y_buffer, y_stride); michael@0: michael@0: constrain_line(lx0, &x1, ly0 + 1, &y1, width, height); michael@0: vp9_blit_line(lx0, x1, ly0 + 1, y1, y_buffer, y_stride); michael@0: } else { michael@0: vp9_blit_line(lx0, x1, ly0, y1, y_buffer, y_stride); michael@0: } michael@0: } michael@0: michael@0: mi++; michael@0: } michael@0: mi++; michael@0: } michael@0: } michael@0: michael@0: /* Color in block modes */ michael@0: if ((flags & VP9D_DEBUG_CLR_BLK_MODES) michael@0: && (ppflags->display_mb_modes_flag || ppflags->display_b_modes_flag)) { michael@0: int y, x; michael@0: YV12_BUFFER_CONFIG *post = &cm->post_proc_buffer; michael@0: int width = post->y_width; michael@0: int height = post->y_height; michael@0: uint8_t *y_ptr = cm->post_proc_buffer.y_buffer; michael@0: uint8_t *u_ptr = cm->post_proc_buffer.u_buffer; michael@0: uint8_t *v_ptr = cm->post_proc_buffer.v_buffer; michael@0: int y_stride = cm->post_proc_buffer.y_stride; michael@0: MODE_INFO *mi = cm->mi; michael@0: michael@0: for (y = 0; y < height; y += 16) { michael@0: for (x = 0; x < width; x += 16) { michael@0: int Y = 0, U = 0, V = 0; michael@0: michael@0: if (mi->mbmi.mode == I4X4_PRED && michael@0: ((ppflags->display_mb_modes_flag & I4X4_PRED) || michael@0: ppflags->display_b_modes_flag)) { michael@0: int by, bx; michael@0: uint8_t *yl, *ul, *vl; michael@0: union b_mode_info *bmi = mi->bmi; michael@0: michael@0: yl = y_ptr + x; michael@0: ul = u_ptr + (x >> 1); michael@0: vl = v_ptr + (x >> 1); michael@0: michael@0: for (by = 0; by < 16; by += 4) { michael@0: for (bx = 0; bx < 16; bx += 4) { michael@0: if ((ppflags->display_b_modes_flag & (1 << mi->mbmi.mode)) michael@0: || (ppflags->display_mb_modes_flag & I4X4_PRED)) { michael@0: Y = B_PREDICTION_MODE_colors[bmi->as_mode][0]; michael@0: U = B_PREDICTION_MODE_colors[bmi->as_mode][1]; michael@0: V = B_PREDICTION_MODE_colors[bmi->as_mode][2]; michael@0: michael@0: vp9_blend_b(yl + bx, ul + (bx >> 1), vl + (bx >> 1), Y, U, V, michael@0: 0xc000, y_stride); michael@0: } michael@0: bmi++; michael@0: } michael@0: michael@0: yl += y_stride * 4; michael@0: ul += y_stride * 1; michael@0: vl += y_stride * 1; michael@0: } michael@0: } else if (ppflags->display_mb_modes_flag & (1 << mi->mbmi.mode)) { michael@0: Y = MB_PREDICTION_MODE_colors[mi->mbmi.mode][0]; michael@0: U = MB_PREDICTION_MODE_colors[mi->mbmi.mode][1]; michael@0: V = MB_PREDICTION_MODE_colors[mi->mbmi.mode][2]; michael@0: michael@0: vp9_blend_mb_inner(y_ptr + x, u_ptr + (x >> 1), v_ptr + (x >> 1), michael@0: Y, U, V, 0xc000, y_stride); michael@0: } michael@0: michael@0: mi++; michael@0: } michael@0: y_ptr += y_stride * 16; michael@0: u_ptr += y_stride * 4; michael@0: v_ptr += y_stride * 4; michael@0: michael@0: mi++; michael@0: } michael@0: } michael@0: michael@0: /* Color in frame reference blocks */ michael@0: if ((flags & VP9D_DEBUG_CLR_FRM_REF_BLKS) && michael@0: ppflags->display_ref_frame_flag) { michael@0: int y, x; michael@0: YV12_BUFFER_CONFIG *post = &cm->post_proc_buffer; michael@0: int width = post->y_width; michael@0: int height = post->y_height; michael@0: uint8_t *y_ptr = cm->post_proc_buffer.y_buffer; michael@0: uint8_t *u_ptr = cm->post_proc_buffer.u_buffer; michael@0: uint8_t *v_ptr = cm->post_proc_buffer.v_buffer; michael@0: int y_stride = cm->post_proc_buffer.y_stride; michael@0: MODE_INFO *mi = cm->mi; michael@0: michael@0: for (y = 0; y < height; y += 16) { michael@0: for (x = 0; x < width; x += 16) { michael@0: int Y = 0, U = 0, V = 0; michael@0: michael@0: if (ppflags->display_ref_frame_flag & (1 << mi->mbmi.ref_frame)) { michael@0: Y = MV_REFERENCE_FRAME_colors[mi->mbmi.ref_frame][0]; michael@0: U = MV_REFERENCE_FRAME_colors[mi->mbmi.ref_frame][1]; michael@0: V = MV_REFERENCE_FRAME_colors[mi->mbmi.ref_frame][2]; michael@0: michael@0: vp9_blend_mb_outer(y_ptr + x, u_ptr + (x >> 1), v_ptr + (x >> 1), michael@0: Y, U, V, 0xc000, y_stride); michael@0: } michael@0: michael@0: mi++; michael@0: } michael@0: y_ptr += y_stride * 16; michael@0: u_ptr += y_stride * 4; michael@0: v_ptr += y_stride * 4; michael@0: michael@0: mi++; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: *dest = cm->post_proc_buffer; michael@0: michael@0: /* handle problem with extending borders */ michael@0: dest->y_width = cm->width; michael@0: dest->y_height = cm->height; michael@0: dest->uv_width = dest->y_width >> cm->subsampling_x; michael@0: dest->uv_height = dest->y_height >> cm->subsampling_y; michael@0: michael@0: return 0; michael@0: }