2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
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.
15 #include "./vpx_config.h"
16 #include "./vpx_scale_rtcd.h"
17 #include "./vp9_rtcd.h"
19 #include "vpx_scale/vpx_scale.h"
20 #include "vpx_scale/yv12config.h"
22 #include "vp9/common/vp9_onyxc_int.h"
23 #include "vp9/common/vp9_postproc.h"
24 #include "vp9/common/vp9_systemdependent.h"
25 #include "vp9/common/vp9_textblit.h"
27 #if CONFIG_VP9_POSTPROC
28 static const short kernel5[] = {
32 const short vp9_rv[] = {
33 8, 5, 2, 2, 8, 12, 4, 9, 8, 3,
34 0, 3, 9, 0, 0, 0, 8, 3, 14, 4,
35 10, 1, 11, 14, 1, 14, 9, 6, 12, 11,
36 8, 6, 10, 0, 0, 8, 9, 0, 3, 14,
37 8, 11, 13, 4, 2, 9, 0, 3, 9, 6,
38 1, 2, 3, 14, 13, 1, 8, 2, 9, 7,
39 3, 3, 1, 13, 13, 6, 6, 5, 2, 7,
40 11, 9, 11, 8, 7, 3, 2, 0, 13, 13,
41 14, 4, 12, 5, 12, 10, 8, 10, 13, 10,
42 4, 14, 4, 10, 0, 8, 11, 1, 13, 7,
43 7, 14, 6, 14, 13, 2, 13, 5, 4, 4,
44 0, 10, 0, 5, 13, 2, 12, 7, 11, 13,
45 8, 0, 4, 10, 7, 2, 7, 2, 2, 5,
46 3, 4, 7, 3, 3, 14, 14, 5, 9, 13,
47 3, 14, 3, 6, 3, 0, 11, 8, 13, 1,
48 13, 1, 12, 0, 10, 9, 7, 6, 2, 8,
49 5, 2, 13, 7, 1, 13, 14, 7, 6, 7,
50 9, 6, 10, 11, 7, 8, 7, 5, 14, 8,
51 4, 4, 0, 8, 7, 10, 0, 8, 14, 11,
52 3, 12, 5, 7, 14, 3, 14, 5, 2, 6,
53 11, 12, 12, 8, 0, 11, 13, 1, 2, 0,
54 5, 10, 14, 7, 8, 0, 4, 11, 0, 8,
55 0, 3, 10, 5, 8, 0, 11, 6, 7, 8,
56 10, 7, 13, 9, 2, 5, 1, 5, 10, 2,
57 4, 3, 5, 6, 10, 8, 9, 4, 11, 14,
58 0, 10, 0, 5, 13, 2, 12, 7, 11, 13,
59 8, 0, 4, 10, 7, 2, 7, 2, 2, 5,
60 3, 4, 7, 3, 3, 14, 14, 5, 9, 13,
61 3, 14, 3, 6, 3, 0, 11, 8, 13, 1,
62 13, 1, 12, 0, 10, 9, 7, 6, 2, 8,
63 5, 2, 13, 7, 1, 13, 14, 7, 6, 7,
64 9, 6, 10, 11, 7, 8, 7, 5, 14, 8,
65 4, 4, 0, 8, 7, 10, 0, 8, 14, 11,
66 3, 12, 5, 7, 14, 3, 14, 5, 2, 6,
67 11, 12, 12, 8, 0, 11, 13, 1, 2, 0,
68 5, 10, 14, 7, 8, 0, 4, 11, 0, 8,
69 0, 3, 10, 5, 8, 0, 11, 6, 7, 8,
70 10, 7, 13, 9, 2, 5, 1, 5, 10, 2,
71 4, 3, 5, 6, 10, 8, 9, 4, 11, 14,
72 3, 8, 3, 7, 8, 5, 11, 4, 12, 3,
73 11, 9, 14, 8, 14, 13, 4, 3, 1, 2,
74 14, 6, 5, 4, 4, 11, 4, 6, 2, 1,
75 5, 8, 8, 12, 13, 5, 14, 10, 12, 13,
76 0, 9, 5, 5, 11, 10, 13, 9, 10, 13,
79 void vp9_post_proc_down_and_across_c(const uint8_t *src_ptr,
81 int src_pixels_per_line,
82 int dst_pixels_per_line,
92 int pitch = src_pixels_per_line;
94 (void)dst_pixels_per_line;
96 for (row = 0; row < rows; row++) {
97 /* post_proc_down for one row */
101 for (col = 0; col < cols; col++) {
105 for (i = -2; i <= 2; i++) {
106 if (abs(v - p_src[col + i * pitch]) > flimit)
107 goto down_skip_convolve;
109 kernel += kernel5[2 + i] * p_src[col + i * pitch];
117 /* now post_proc_across */
121 for (i = 0; i < 8; i++)
124 for (col = 0; col < cols; col++) {
130 for (i = -2; i <= 2; i++) {
131 if (abs(v - p_src[col + i]) > flimit)
132 goto across_skip_convolve;
134 kernel += kernel5[2 + i] * p_src[col + i];
137 d[col & 7] = (kernel >> 3);
138 across_skip_convolve:
141 p_dst[col - 2] = d[(col - 2) & 7];
144 /* handle the last two pixels */
145 p_dst[col - 2] = d[(col - 2) & 7];
146 p_dst[col - 1] = d[(col - 1) & 7];
155 static int q2mbl(int x) {
158 x = 50 + (x - 50) * 10 / 8;
162 void vp9_mbpost_proc_across_ip_c(uint8_t *src, int pitch,
163 int rows, int cols, int flimit) {
170 for (r = 0; r < rows; r++) {
174 for (i = -8; i <= 6; i++) {
175 sumsq += s[i] * s[i];
180 for (c = 0; c < cols + 8; c++) {
181 int x = s[c + 7] - s[c - 8];
182 int y = s[c + 7] + s[c - 8];
189 if (sumsq * 15 - sum * sum < flimit) {
190 d[c & 15] = (8 + sum + s[c]) >> 4;
193 s[c - 8] = d[(c - 8) & 15];
200 void vp9_mbpost_proc_down_c(uint8_t *dst, int pitch,
201 int rows, int cols, int flimit) {
203 const short *rv3 = &vp9_rv[63 & rand()]; // NOLINT
205 for (c = 0; c < cols; c++) {
206 uint8_t *s = &dst[c];
210 const short *rv2 = rv3 + ((c * 17) & 127);
212 for (i = -8; i <= 6; i++) {
213 sumsq += s[i * pitch] * s[i * pitch];
217 for (r = 0; r < rows + 8; r++) {
218 sumsq += s[7 * pitch] * s[ 7 * pitch] - s[-8 * pitch] * s[-8 * pitch];
219 sum += s[7 * pitch] - s[-8 * pitch];
222 if (sumsq * 15 - sum * sum < flimit) {
223 d[r & 15] = (rv2[r & 127] + sum + s[0]) >> 4;
226 s[-8 * pitch] = d[(r - 8) & 15];
232 static void deblock_and_de_macro_block(YV12_BUFFER_CONFIG *source,
233 YV12_BUFFER_CONFIG *post,
237 double level = 6.0e-05 * q * q * q - .0067 * q * q + .306 * q + .0065;
238 int ppl = (int)(level + .5);
239 (void) low_var_thresh;
242 vp9_post_proc_down_and_across(source->y_buffer, post->y_buffer,
243 source->y_stride, post->y_stride,
244 source->y_height, source->y_width, ppl);
246 vp9_mbpost_proc_across_ip(post->y_buffer, post->y_stride, post->y_height,
247 post->y_width, q2mbl(q));
249 vp9_mbpost_proc_down(post->y_buffer, post->y_stride, post->y_height,
250 post->y_width, q2mbl(q));
252 vp9_post_proc_down_and_across(source->u_buffer, post->u_buffer,
253 source->uv_stride, post->uv_stride,
254 source->uv_height, source->uv_width, ppl);
255 vp9_post_proc_down_and_across(source->v_buffer, post->v_buffer,
256 source->uv_stride, post->uv_stride,
257 source->uv_height, source->uv_width, ppl);
260 void vp9_deblock(const YV12_BUFFER_CONFIG *src, YV12_BUFFER_CONFIG *dst,
262 const int ppl = (int)(6.0e-05 * q * q * q - 0.0067 * q * q + 0.306 * q
266 const uint8_t *const srcs[3] = {src->y_buffer, src->u_buffer, src->v_buffer};
267 const int src_strides[3] = {src->y_stride, src->uv_stride, src->uv_stride};
268 const int src_widths[3] = {src->y_width, src->uv_width, src->uv_width};
269 const int src_heights[3] = {src->y_height, src->uv_height, src->uv_height};
271 uint8_t *const dsts[3] = {dst->y_buffer, dst->u_buffer, dst->v_buffer};
272 const int dst_strides[3] = {dst->y_stride, dst->uv_stride, dst->uv_stride};
274 for (i = 0; i < MAX_MB_PLANE; ++i)
275 vp9_post_proc_down_and_across(srcs[i], dsts[i],
276 src_strides[i], dst_strides[i],
277 src_heights[i], src_widths[i], ppl);
280 void vp9_denoise(const YV12_BUFFER_CONFIG *src, YV12_BUFFER_CONFIG *dst,
282 const int ppl = (int)(6.0e-05 * q * q * q - 0.0067 * q * q + 0.306 * q
286 const uint8_t *const srcs[3] = {src->y_buffer, src->u_buffer, src->v_buffer};
287 const int src_strides[3] = {src->y_stride, src->uv_stride, src->uv_stride};
288 const int src_widths[3] = {src->y_width, src->uv_width, src->uv_width};
289 const int src_heights[3] = {src->y_height, src->uv_height, src->uv_height};
291 uint8_t *const dsts[3] = {dst->y_buffer, dst->u_buffer, dst->v_buffer};
292 const int dst_strides[3] = {dst->y_stride, dst->uv_stride, dst->uv_stride};
294 for (i = 0; i < MAX_MB_PLANE; ++i) {
295 const int src_stride = src_strides[i];
296 const uint8_t *const src = srcs[i] + 2 * src_stride + 2;
297 const int src_width = src_widths[i] - 4;
298 const int src_height = src_heights[i] - 4;
300 const int dst_stride = dst_strides[i];
301 uint8_t *const dst = dsts[i] + 2 * dst_stride + 2;
303 vp9_post_proc_down_and_across(src, dst, src_stride, dst_stride,
304 src_height, src_width, ppl);
308 static double gaussian(double sigma, double mu, double x) {
309 return 1 / (sigma * sqrt(2.0 * 3.14159265)) *
310 (exp(-(x - mu) * (x - mu) / (2 * sigma * sigma)));
313 static void fillrd(struct postproc_state *state, int q, int a) {
317 int ai = a, qi = q, i;
319 vp9_clear_system_state();
321 sigma = ai + .5 + .6 * (63 - qi) / 63.0;
323 /* set up a lookup table of 256 entries that matches
324 * a gaussian distribution with sigma determined by q.
332 for (i = -32; i < 32; i++) {
333 int a = (int)(0.5 + 256 * gaussian(sigma, 0, i));
336 for (j = 0; j < a; j++) {
337 char_dist[next + j] = (char) i;
344 for (; next < 256; next++)
348 for (i = 0; i < 3072; i++) {
349 state->noise[i] = char_dist[rand() & 0xff]; // NOLINT
352 for (i = 0; i < 16; i++) {
353 state->blackclamp[i] = -char_dist[0];
354 state->whiteclamp[i] = -char_dist[0];
355 state->bothclamp[i] = -2 * char_dist[0];
359 state->last_noise = a;
362 void vp9_plane_add_noise_c(uint8_t *start, char *noise,
366 unsigned int width, unsigned int height, int pitch) {
369 // TODO(jbb): why does simd code use both but c doesn't, normalize and
372 for (i = 0; i < height; i++) {
373 uint8_t *pos = start + i * pitch;
374 char *ref = (char *)(noise + (rand() & 0xff)); // NOLINT
376 for (j = 0; j < width; j++) {
377 if (pos[j] < blackclamp[0])
378 pos[j] = blackclamp[0];
380 if (pos[j] > 255 + whiteclamp[0])
381 pos[j] = 255 + whiteclamp[0];
388 int vp9_post_proc_frame(struct VP9Common *cm,
389 YV12_BUFFER_CONFIG *dest, vp9_ppflags_t *ppflags) {
390 const int q = MIN(63, cm->lf.filter_level * 10 / 6);
391 const int flags = ppflags->post_proc_flag;
392 YV12_BUFFER_CONFIG *const ppbuf = &cm->post_proc_buffer;
393 struct postproc_state *const ppstate = &cm->postproc_state;
395 if (!cm->frame_to_show)
399 *dest = *cm->frame_to_show;
403 vp9_clear_system_state();
405 #if CONFIG_VP9_POSTPROC || CONFIG_INTERNAL_STATS
406 if (vp9_realloc_frame_buffer(&cm->post_proc_buffer, cm->width, cm->height,
407 cm->subsampling_x, cm->subsampling_y,
408 VP9_DEC_BORDER_IN_PIXELS, NULL, NULL, NULL) < 0)
409 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
410 "Failed to allocate post-processing buffer");
413 if (flags & VP9D_DEMACROBLOCK) {
414 deblock_and_de_macro_block(cm->frame_to_show, ppbuf,
415 q + (ppflags->deblocking_level - 5) * 10, 1, 0);
416 } else if (flags & VP9D_DEBLOCK) {
417 vp9_deblock(cm->frame_to_show, ppbuf, q);
419 vp8_yv12_copy_frame(cm->frame_to_show, ppbuf);
422 if (flags & VP9D_ADDNOISE) {
423 const int noise_level = ppflags->noise_level;
424 if (ppstate->last_q != q ||
425 ppstate->last_noise != noise_level) {
426 fillrd(ppstate, 63 - q, noise_level);
429 vp9_plane_add_noise(ppbuf->y_buffer, ppstate->noise, ppstate->blackclamp,
430 ppstate->whiteclamp, ppstate->bothclamp,
431 ppbuf->y_width, ppbuf->y_height, ppbuf->y_stride);
436 /* handle problem with extending borders */
437 dest->y_width = cm->width;
438 dest->y_height = cm->height;
439 dest->uv_width = dest->y_width >> cm->subsampling_x;
440 dest->uv_height = dest->y_height >> cm->subsampling_y;