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 #if CONFIG_VP9_HIGHBITDEPTH
23 #include "vp9/common/vp9_common.h"
25 #include "vp9/common/vp9_onyxc_int.h"
26 #include "vp9/common/vp9_postproc.h"
27 #include "vp9/common/vp9_systemdependent.h"
28 #include "vp9/common/vp9_textblit.h"
30 #if CONFIG_VP9_POSTPROC
31 static const short kernel5[] = {
35 const short vp9_rv[] = {
36 8, 5, 2, 2, 8, 12, 4, 9, 8, 3,
37 0, 3, 9, 0, 0, 0, 8, 3, 14, 4,
38 10, 1, 11, 14, 1, 14, 9, 6, 12, 11,
39 8, 6, 10, 0, 0, 8, 9, 0, 3, 14,
40 8, 11, 13, 4, 2, 9, 0, 3, 9, 6,
41 1, 2, 3, 14, 13, 1, 8, 2, 9, 7,
42 3, 3, 1, 13, 13, 6, 6, 5, 2, 7,
43 11, 9, 11, 8, 7, 3, 2, 0, 13, 13,
44 14, 4, 12, 5, 12, 10, 8, 10, 13, 10,
45 4, 14, 4, 10, 0, 8, 11, 1, 13, 7,
46 7, 14, 6, 14, 13, 2, 13, 5, 4, 4,
47 0, 10, 0, 5, 13, 2, 12, 7, 11, 13,
48 8, 0, 4, 10, 7, 2, 7, 2, 2, 5,
49 3, 4, 7, 3, 3, 14, 14, 5, 9, 13,
50 3, 14, 3, 6, 3, 0, 11, 8, 13, 1,
51 13, 1, 12, 0, 10, 9, 7, 6, 2, 8,
52 5, 2, 13, 7, 1, 13, 14, 7, 6, 7,
53 9, 6, 10, 11, 7, 8, 7, 5, 14, 8,
54 4, 4, 0, 8, 7, 10, 0, 8, 14, 11,
55 3, 12, 5, 7, 14, 3, 14, 5, 2, 6,
56 11, 12, 12, 8, 0, 11, 13, 1, 2, 0,
57 5, 10, 14, 7, 8, 0, 4, 11, 0, 8,
58 0, 3, 10, 5, 8, 0, 11, 6, 7, 8,
59 10, 7, 13, 9, 2, 5, 1, 5, 10, 2,
60 4, 3, 5, 6, 10, 8, 9, 4, 11, 14,
61 0, 10, 0, 5, 13, 2, 12, 7, 11, 13,
62 8, 0, 4, 10, 7, 2, 7, 2, 2, 5,
63 3, 4, 7, 3, 3, 14, 14, 5, 9, 13,
64 3, 14, 3, 6, 3, 0, 11, 8, 13, 1,
65 13, 1, 12, 0, 10, 9, 7, 6, 2, 8,
66 5, 2, 13, 7, 1, 13, 14, 7, 6, 7,
67 9, 6, 10, 11, 7, 8, 7, 5, 14, 8,
68 4, 4, 0, 8, 7, 10, 0, 8, 14, 11,
69 3, 12, 5, 7, 14, 3, 14, 5, 2, 6,
70 11, 12, 12, 8, 0, 11, 13, 1, 2, 0,
71 5, 10, 14, 7, 8, 0, 4, 11, 0, 8,
72 0, 3, 10, 5, 8, 0, 11, 6, 7, 8,
73 10, 7, 13, 9, 2, 5, 1, 5, 10, 2,
74 4, 3, 5, 6, 10, 8, 9, 4, 11, 14,
75 3, 8, 3, 7, 8, 5, 11, 4, 12, 3,
76 11, 9, 14, 8, 14, 13, 4, 3, 1, 2,
77 14, 6, 5, 4, 4, 11, 4, 6, 2, 1,
78 5, 8, 8, 12, 13, 5, 14, 10, 12, 13,
79 0, 9, 5, 5, 11, 10, 13, 9, 10, 13,
82 void vp9_post_proc_down_and_across_c(const uint8_t *src_ptr,
84 int src_pixels_per_line,
85 int dst_pixels_per_line,
95 int pitch = src_pixels_per_line;
97 (void)dst_pixels_per_line;
99 for (row = 0; row < rows; row++) {
100 /* post_proc_down for one row */
104 for (col = 0; col < cols; col++) {
108 for (i = -2; i <= 2; i++) {
109 if (abs(v - p_src[col + i * pitch]) > flimit)
110 goto down_skip_convolve;
112 kernel += kernel5[2 + i] * p_src[col + i * pitch];
120 /* now post_proc_across */
124 for (i = 0; i < 8; i++)
127 for (col = 0; col < cols; col++) {
133 for (i = -2; i <= 2; i++) {
134 if (abs(v - p_src[col + i]) > flimit)
135 goto across_skip_convolve;
137 kernel += kernel5[2 + i] * p_src[col + i];
140 d[col & 7] = (kernel >> 3);
141 across_skip_convolve:
144 p_dst[col - 2] = d[(col - 2) & 7];
147 /* handle the last two pixels */
148 p_dst[col - 2] = d[(col - 2) & 7];
149 p_dst[col - 1] = d[(col - 1) & 7];
158 #if CONFIG_VP9_HIGHBITDEPTH
159 void vp9_highbd_post_proc_down_and_across_c(const uint16_t *src_ptr,
161 int src_pixels_per_line,
162 int dst_pixels_per_line,
166 uint16_t const *p_src;
172 int pitch = src_pixels_per_line;
175 for (row = 0; row < rows; row++) {
176 // post_proc_down for one row.
180 for (col = 0; col < cols; col++) {
184 for (i = -2; i <= 2; i++) {
185 if (abs(v - p_src[col + i * pitch]) > flimit)
186 goto down_skip_convolve;
188 kernel += kernel5[2 + i] * p_src[col + i * pitch];
197 /* now post_proc_across */
201 for (i = 0; i < 8; i++)
204 for (col = 0; col < cols; col++) {
210 for (i = -2; i <= 2; i++) {
211 if (abs(v - p_src[col + i]) > flimit)
212 goto across_skip_convolve;
214 kernel += kernel5[2 + i] * p_src[col + i];
217 d[col & 7] = (kernel >> 3);
219 across_skip_convolve:
221 p_dst[col - 2] = d[(col - 2) & 7];
224 /* handle the last two pixels */
225 p_dst[col - 2] = d[(col - 2) & 7];
226 p_dst[col - 1] = d[(col - 1) & 7];
231 dst_ptr += dst_pixels_per_line;
234 #endif // CONFIG_VP9_HIGHBITDEPTH
236 static int q2mbl(int x) {
239 x = 50 + (x - 50) * 10 / 8;
243 void vp9_mbpost_proc_across_ip_c(uint8_t *src, int pitch,
244 int rows, int cols, int flimit) {
249 for (r = 0; r < rows; r++) {
253 for (i = -8; i <= 6; i++) {
254 sumsq += s[i] * s[i];
259 for (c = 0; c < cols + 8; c++) {
260 int x = s[c + 7] - s[c - 8];
261 int y = s[c + 7] + s[c - 8];
268 if (sumsq * 15 - sum * sum < flimit) {
269 d[c & 15] = (8 + sum + s[c]) >> 4;
272 s[c - 8] = d[(c - 8) & 15];
278 #if CONFIG_VP9_HIGHBITDEPTH
279 void vp9_highbd_mbpost_proc_across_ip_c(uint16_t *src, int pitch,
280 int rows, int cols, int flimit) {
287 for (r = 0; r < rows; r++) {
291 for (i = -8; i <= 6; i++) {
292 sumsq += s[i] * s[i];
297 for (c = 0; c < cols + 8; c++) {
298 int x = s[c + 7] - s[c - 8];
299 int y = s[c + 7] + s[c - 8];
306 if (sumsq * 15 - sum * sum < flimit) {
307 d[c & 15] = (8 + sum + s[c]) >> 4;
310 s[c - 8] = d[(c - 8) & 15];
316 #endif // CONFIG_VP9_HIGHBITDEPTH
318 void vp9_mbpost_proc_down_c(uint8_t *dst, int pitch,
319 int rows, int cols, int flimit) {
321 const short *rv3 = &vp9_rv[63 & rand()]; // NOLINT
323 for (c = 0; c < cols; c++) {
324 uint8_t *s = &dst[c];
328 const short *rv2 = rv3 + ((c * 17) & 127);
330 for (i = -8; i <= 6; i++) {
331 sumsq += s[i * pitch] * s[i * pitch];
335 for (r = 0; r < rows + 8; r++) {
336 sumsq += s[7 * pitch] * s[ 7 * pitch] - s[-8 * pitch] * s[-8 * pitch];
337 sum += s[7 * pitch] - s[-8 * pitch];
340 if (sumsq * 15 - sum * sum < flimit) {
341 d[r & 15] = (rv2[r & 127] + sum + s[0]) >> 4;
344 s[-8 * pitch] = d[(r - 8) & 15];
350 #if CONFIG_VP9_HIGHBITDEPTH
351 void vp9_highbd_mbpost_proc_down_c(uint16_t *dst, int pitch,
352 int rows, int cols, int flimit) {
354 const int16_t *rv3 = &vp9_rv[63 & rand()]; // NOLINT
356 for (c = 0; c < cols; c++) {
357 uint16_t *s = &dst[c];
361 const int16_t *rv2 = rv3 + ((c * 17) & 127);
363 for (i = -8; i <= 6; i++) {
364 sumsq += s[i * pitch] * s[i * pitch];
368 for (r = 0; r < rows + 8; r++) {
369 sumsq += s[7 * pitch] * s[ 7 * pitch] - s[-8 * pitch] * s[-8 * pitch];
370 sum += s[7 * pitch] - s[-8 * pitch];
373 if (sumsq * 15 - sum * sum < flimit) {
374 d[r & 15] = (rv2[r & 127] + sum + s[0]) >> 4;
377 s[-8 * pitch] = d[(r - 8) & 15];
382 #endif // CONFIG_VP9_HIGHBITDEPTH
384 static void deblock_and_de_macro_block(YV12_BUFFER_CONFIG *source,
385 YV12_BUFFER_CONFIG *post,
389 double level = 6.0e-05 * q * q * q - .0067 * q * q + .306 * q + .0065;
390 int ppl = (int)(level + .5);
391 (void) low_var_thresh;
394 #if CONFIG_VP9_HIGHBITDEPTH
395 if (source->flags & YV12_FLAG_HIGHBITDEPTH) {
396 vp9_highbd_post_proc_down_and_across(CONVERT_TO_SHORTPTR(source->y_buffer),
397 CONVERT_TO_SHORTPTR(post->y_buffer),
398 source->y_stride, post->y_stride,
399 source->y_height, source->y_width,
402 vp9_highbd_mbpost_proc_across_ip(CONVERT_TO_SHORTPTR(post->y_buffer),
403 post->y_stride, post->y_height,
404 post->y_width, q2mbl(q));
406 vp9_highbd_mbpost_proc_down(CONVERT_TO_SHORTPTR(post->y_buffer),
407 post->y_stride, post->y_height,
408 post->y_width, q2mbl(q));
410 vp9_highbd_post_proc_down_and_across(CONVERT_TO_SHORTPTR(source->u_buffer),
411 CONVERT_TO_SHORTPTR(post->u_buffer),
412 source->uv_stride, post->uv_stride,
413 source->uv_height, source->uv_width,
415 vp9_highbd_post_proc_down_and_across(CONVERT_TO_SHORTPTR(source->v_buffer),
416 CONVERT_TO_SHORTPTR(post->v_buffer),
417 source->uv_stride, post->uv_stride,
418 source->uv_height, source->uv_width,
421 vp9_post_proc_down_and_across(source->y_buffer, post->y_buffer,
422 source->y_stride, post->y_stride,
423 source->y_height, source->y_width, ppl);
425 vp9_mbpost_proc_across_ip(post->y_buffer, post->y_stride, post->y_height,
426 post->y_width, q2mbl(q));
428 vp9_mbpost_proc_down(post->y_buffer, post->y_stride, post->y_height,
429 post->y_width, q2mbl(q));
431 vp9_post_proc_down_and_across(source->u_buffer, post->u_buffer,
432 source->uv_stride, post->uv_stride,
433 source->uv_height, source->uv_width, ppl);
434 vp9_post_proc_down_and_across(source->v_buffer, post->v_buffer,
435 source->uv_stride, post->uv_stride,
436 source->uv_height, source->uv_width, ppl);
439 vp9_post_proc_down_and_across(source->y_buffer, post->y_buffer,
440 source->y_stride, post->y_stride,
441 source->y_height, source->y_width, ppl);
443 vp9_mbpost_proc_across_ip(post->y_buffer, post->y_stride, post->y_height,
444 post->y_width, q2mbl(q));
446 vp9_mbpost_proc_down(post->y_buffer, post->y_stride, post->y_height,
447 post->y_width, q2mbl(q));
449 vp9_post_proc_down_and_across(source->u_buffer, post->u_buffer,
450 source->uv_stride, post->uv_stride,
451 source->uv_height, source->uv_width, ppl);
452 vp9_post_proc_down_and_across(source->v_buffer, post->v_buffer,
453 source->uv_stride, post->uv_stride,
454 source->uv_height, source->uv_width, ppl);
455 #endif // CONFIG_VP9_HIGHBITDEPTH
458 void vp9_deblock(const YV12_BUFFER_CONFIG *src, YV12_BUFFER_CONFIG *dst,
460 const int ppl = (int)(6.0e-05 * q * q * q - 0.0067 * q * q + 0.306 * q
464 const uint8_t *const srcs[3] = {src->y_buffer, src->u_buffer, src->v_buffer};
465 const int src_strides[3] = {src->y_stride, src->uv_stride, src->uv_stride};
466 const int src_widths[3] = {src->y_width, src->uv_width, src->uv_width};
467 const int src_heights[3] = {src->y_height, src->uv_height, src->uv_height};
469 uint8_t *const dsts[3] = {dst->y_buffer, dst->u_buffer, dst->v_buffer};
470 const int dst_strides[3] = {dst->y_stride, dst->uv_stride, dst->uv_stride};
472 for (i = 0; i < MAX_MB_PLANE; ++i) {
473 #if CONFIG_VP9_HIGHBITDEPTH
474 assert((src->flags & YV12_FLAG_HIGHBITDEPTH) ==
475 (dst->flags & YV12_FLAG_HIGHBITDEPTH));
476 if (src->flags & YV12_FLAG_HIGHBITDEPTH) {
477 vp9_highbd_post_proc_down_and_across(CONVERT_TO_SHORTPTR(srcs[i]),
478 CONVERT_TO_SHORTPTR(dsts[i]),
479 src_strides[i], dst_strides[i],
480 src_heights[i], src_widths[i], ppl);
482 vp9_post_proc_down_and_across(srcs[i], dsts[i],
483 src_strides[i], dst_strides[i],
484 src_heights[i], src_widths[i], ppl);
487 vp9_post_proc_down_and_across(srcs[i], dsts[i],
488 src_strides[i], dst_strides[i],
489 src_heights[i], src_widths[i], ppl);
490 #endif // CONFIG_VP9_HIGHBITDEPTH
494 void vp9_denoise(const YV12_BUFFER_CONFIG *src, YV12_BUFFER_CONFIG *dst,
496 const int ppl = (int)(6.0e-05 * q * q * q - 0.0067 * q * q + 0.306 * q
500 const uint8_t *const srcs[3] = {src->y_buffer, src->u_buffer, src->v_buffer};
501 const int src_strides[3] = {src->y_stride, src->uv_stride, src->uv_stride};
502 const int src_widths[3] = {src->y_width, src->uv_width, src->uv_width};
503 const int src_heights[3] = {src->y_height, src->uv_height, src->uv_height};
505 uint8_t *const dsts[3] = {dst->y_buffer, dst->u_buffer, dst->v_buffer};
506 const int dst_strides[3] = {dst->y_stride, dst->uv_stride, dst->uv_stride};
508 for (i = 0; i < MAX_MB_PLANE; ++i) {
509 const int src_stride = src_strides[i];
510 const int src_width = src_widths[i] - 4;
511 const int src_height = src_heights[i] - 4;
512 const int dst_stride = dst_strides[i];
514 #if CONFIG_VP9_HIGHBITDEPTH
515 assert((src->flags & YV12_FLAG_HIGHBITDEPTH) ==
516 (dst->flags & YV12_FLAG_HIGHBITDEPTH));
517 if (src->flags & YV12_FLAG_HIGHBITDEPTH) {
518 const uint16_t *const src = CONVERT_TO_SHORTPTR(srcs[i] + 2 * src_stride
520 uint16_t *const dst = CONVERT_TO_SHORTPTR(dsts[i] + 2 * dst_stride + 2);
521 vp9_highbd_post_proc_down_and_across(src, dst, src_stride, dst_stride,
522 src_height, src_width, ppl);
524 const uint8_t *const src = srcs[i] + 2 * src_stride + 2;
525 uint8_t *const dst = dsts[i] + 2 * dst_stride + 2;
527 vp9_post_proc_down_and_across(src, dst, src_stride, dst_stride,
528 src_height, src_width, ppl);
531 const uint8_t *const src = srcs[i] + 2 * src_stride + 2;
532 uint8_t *const dst = dsts[i] + 2 * dst_stride + 2;
533 vp9_post_proc_down_and_across(src, dst, src_stride, dst_stride,
534 src_height, src_width, ppl);
539 static double gaussian(double sigma, double mu, double x) {
540 return 1 / (sigma * sqrt(2.0 * 3.14159265)) *
541 (exp(-(x - mu) * (x - mu) / (2 * sigma * sigma)));
544 static void fillrd(struct postproc_state *state, int q, int a) {
548 int ai = a, qi = q, i;
550 vp9_clear_system_state();
552 sigma = ai + .5 + .6 * (63 - qi) / 63.0;
554 /* set up a lookup table of 256 entries that matches
555 * a gaussian distribution with sigma determined by q.
563 for (i = -32; i < 32; i++) {
564 int a = (int)(0.5 + 256 * gaussian(sigma, 0, i));
567 for (j = 0; j < a; j++) {
568 char_dist[next + j] = (char) i;
575 for (; next < 256; next++)
579 for (i = 0; i < 3072; i++) {
580 state->noise[i] = char_dist[rand() & 0xff]; // NOLINT
583 for (i = 0; i < 16; i++) {
584 state->blackclamp[i] = -char_dist[0];
585 state->whiteclamp[i] = -char_dist[0];
586 state->bothclamp[i] = -2 * char_dist[0];
590 state->last_noise = a;
593 void vp9_plane_add_noise_c(uint8_t *start, char *noise,
597 unsigned int width, unsigned int height, int pitch) {
600 // TODO(jbb): why does simd code use both but c doesn't, normalize and
603 for (i = 0; i < height; i++) {
604 uint8_t *pos = start + i * pitch;
605 char *ref = (char *)(noise + (rand() & 0xff)); // NOLINT
607 for (j = 0; j < width; j++) {
608 if (pos[j] < blackclamp[0])
609 pos[j] = blackclamp[0];
611 if (pos[j] > 255 + whiteclamp[0])
612 pos[j] = 255 + whiteclamp[0];
619 int vp9_post_proc_frame(struct VP9Common *cm,
620 YV12_BUFFER_CONFIG *dest, vp9_ppflags_t *ppflags) {
621 const int q = MIN(63, cm->lf.filter_level * 10 / 6);
622 const int flags = ppflags->post_proc_flag;
623 YV12_BUFFER_CONFIG *const ppbuf = &cm->post_proc_buffer;
624 struct postproc_state *const ppstate = &cm->postproc_state;
626 if (!cm->frame_to_show)
630 *dest = *cm->frame_to_show;
634 vp9_clear_system_state();
636 #if CONFIG_VP9_POSTPROC || CONFIG_INTERNAL_STATS
637 if (vp9_realloc_frame_buffer(&cm->post_proc_buffer, cm->width, cm->height,
638 cm->subsampling_x, cm->subsampling_y,
639 #if CONFIG_VP9_HIGHBITDEPTH
640 cm->use_highbitdepth,
642 VP9_DEC_BORDER_IN_PIXELS, NULL, NULL, NULL) < 0)
643 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
644 "Failed to allocate post-processing buffer");
647 if (flags & VP9D_DEMACROBLOCK) {
648 deblock_and_de_macro_block(cm->frame_to_show, ppbuf,
649 q + (ppflags->deblocking_level - 5) * 10, 1, 0);
650 } else if (flags & VP9D_DEBLOCK) {
651 vp9_deblock(cm->frame_to_show, ppbuf, q);
653 vp8_yv12_copy_frame(cm->frame_to_show, ppbuf);
656 if (flags & VP9D_ADDNOISE) {
657 const int noise_level = ppflags->noise_level;
658 if (ppstate->last_q != q ||
659 ppstate->last_noise != noise_level) {
660 fillrd(ppstate, 63 - q, noise_level);
663 vp9_plane_add_noise(ppbuf->y_buffer, ppstate->noise, ppstate->blackclamp,
664 ppstate->whiteclamp, ppstate->bothclamp,
665 ppbuf->y_width, ppbuf->y_height, ppbuf->y_stride);
670 /* handle problem with extending borders */
671 dest->y_width = cm->width;
672 dest->y_height = cm->height;
673 dest->uv_width = dest->y_width >> cm->subsampling_x;
674 dest->uv_height = dest->y_height >> cm->subsampling_y;