1 /*
2  *  Copyright (c) 2010 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 
11 #include <math.h>
12 #include <stdlib.h>
13 #include <stdio.h>
14 
15 #include "./vpx_config.h"
16 #include "./vpx_scale_rtcd.h"
17 #include "./vp9_rtcd.h"
18 
19 #include "vpx_scale/vpx_scale.h"
20 #include "vpx_scale/yv12config.h"
21 
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"
26 
27 #if CONFIG_VP9_POSTPROC
28 static const short kernel5[] = {
29   1, 1, 4, 1, 1
30 };
31 
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,
77 };
78 
vp9_post_proc_down_and_across_c(const uint8_t * src_ptr,uint8_t * dst_ptr,int src_pixels_per_line,int dst_pixels_per_line,int rows,int cols,int flimit)79 void vp9_post_proc_down_and_across_c(const uint8_t *src_ptr,
80                                      uint8_t *dst_ptr,
81                                      int src_pixels_per_line,
82                                      int dst_pixels_per_line,
83                                      int rows,
84                                      int cols,
85                                      int flimit) {
86   uint8_t const *p_src;
87   uint8_t *p_dst;
88   int row;
89   int col;
90   int i;
91   int v;
92   int pitch = src_pixels_per_line;
93   uint8_t d[8];
94   (void)dst_pixels_per_line;
95 
96   for (row = 0; row < rows; row++) {
97     /* post_proc_down for one row */
98     p_src = src_ptr;
99     p_dst = dst_ptr;
100 
101     for (col = 0; col < cols; col++) {
102       int kernel = 4;
103       int v = p_src[col];
104 
105       for (i = -2; i <= 2; i++) {
106         if (abs(v - p_src[col + i * pitch]) > flimit)
107           goto down_skip_convolve;
108 
109         kernel += kernel5[2 + i] * p_src[col + i * pitch];
110       }
111 
112       v = (kernel >> 3);
113     down_skip_convolve:
114       p_dst[col] = v;
115     }
116 
117     /* now post_proc_across */
118     p_src = dst_ptr;
119     p_dst = dst_ptr;
120 
121     for (i = 0; i < 8; i++)
122       d[i] = p_src[i];
123 
124     for (col = 0; col < cols; col++) {
125       int kernel = 4;
126       v = p_src[col];
127 
128       d[col & 7] = v;
129 
130       for (i = -2; i <= 2; i++) {
131         if (abs(v - p_src[col + i]) > flimit)
132           goto across_skip_convolve;
133 
134         kernel += kernel5[2 + i] * p_src[col + i];
135       }
136 
137       d[col & 7] = (kernel >> 3);
138     across_skip_convolve:
139 
140       if (col >= 2)
141         p_dst[col - 2] = d[(col - 2) & 7];
142     }
143 
144     /* handle the last two pixels */
145     p_dst[col - 2] = d[(col - 2) & 7];
146     p_dst[col - 1] = d[(col - 1) & 7];
147 
148 
149     /* next row */
150     src_ptr += pitch;
151     dst_ptr += pitch;
152   }
153 }
154 
q2mbl(int x)155 static int q2mbl(int x) {
156   if (x < 20) x = 20;
157 
158   x = 50 + (x - 50) * 10 / 8;
159   return x * x / 3;
160 }
161 
vp9_mbpost_proc_across_ip_c(uint8_t * src,int pitch,int rows,int cols,int flimit)162 void vp9_mbpost_proc_across_ip_c(uint8_t *src, int pitch,
163                                  int rows, int cols, int flimit) {
164   int r, c, i;
165 
166   uint8_t *s = src;
167   uint8_t d[16];
168 
169 
170   for (r = 0; r < rows; r++) {
171     int sumsq = 0;
172     int sum   = 0;
173 
174     for (i = -8; i <= 6; i++) {
175       sumsq += s[i] * s[i];
176       sum   += s[i];
177       d[i + 8] = 0;
178     }
179 
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];
183 
184       sum  += x;
185       sumsq += x * y;
186 
187       d[c & 15] = s[c];
188 
189       if (sumsq * 15 - sum * sum < flimit) {
190         d[c & 15] = (8 + sum + s[c]) >> 4;
191       }
192 
193       s[c - 8] = d[(c - 8) & 15];
194     }
195 
196     s += pitch;
197   }
198 }
199 
vp9_mbpost_proc_down_c(uint8_t * dst,int pitch,int rows,int cols,int flimit)200 void vp9_mbpost_proc_down_c(uint8_t *dst, int pitch,
201                             int rows, int cols, int flimit) {
202   int r, c, i;
203   const short *rv3 = &vp9_rv[63 & rand()]; // NOLINT
204 
205   for (c = 0; c < cols; c++) {
206     uint8_t *s = &dst[c];
207     int sumsq = 0;
208     int sum   = 0;
209     uint8_t d[16];
210     const short *rv2 = rv3 + ((c * 17) & 127);
211 
212     for (i = -8; i <= 6; i++) {
213       sumsq += s[i * pitch] * s[i * pitch];
214       sum   += s[i * pitch];
215     }
216 
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];
220       d[r & 15] = s[0];
221 
222       if (sumsq * 15 - sum * sum < flimit) {
223         d[r & 15] = (rv2[r & 127] + sum + s[0]) >> 4;
224       }
225 
226       s[-8 * pitch] = d[(r - 8) & 15];
227       s += pitch;
228     }
229   }
230 }
231 
deblock_and_de_macro_block(YV12_BUFFER_CONFIG * source,YV12_BUFFER_CONFIG * post,int q,int low_var_thresh,int flag)232 static void deblock_and_de_macro_block(YV12_BUFFER_CONFIG   *source,
233                                        YV12_BUFFER_CONFIG   *post,
234                                        int                   q,
235                                        int                   low_var_thresh,
236                                        int                   flag) {
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;
240   (void) flag;
241 
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);
245 
246   vp9_mbpost_proc_across_ip(post->y_buffer, post->y_stride, post->y_height,
247                             post->y_width, q2mbl(q));
248 
249   vp9_mbpost_proc_down(post->y_buffer, post->y_stride, post->y_height,
250                        post->y_width, q2mbl(q));
251 
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);
258 }
259 
vp9_deblock(const YV12_BUFFER_CONFIG * src,YV12_BUFFER_CONFIG * dst,int q)260 void vp9_deblock(const YV12_BUFFER_CONFIG *src, YV12_BUFFER_CONFIG *dst,
261                  int q) {
262   const int ppl = (int)(6.0e-05 * q * q * q - 0.0067 * q * q + 0.306 * q
263                         + 0.0065 + 0.5);
264   int i;
265 
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};
270 
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};
273 
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);
278 }
279 
vp9_denoise(const YV12_BUFFER_CONFIG * src,YV12_BUFFER_CONFIG * dst,int q)280 void vp9_denoise(const YV12_BUFFER_CONFIG *src, YV12_BUFFER_CONFIG *dst,
281                  int q) {
282   const int ppl = (int)(6.0e-05 * q * q * q - 0.0067 * q * q + 0.306 * q
283                         + 0.0065 + 0.5);
284   int i;
285 
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};
290 
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};
293 
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;
299 
300     const int dst_stride = dst_strides[i];
301     uint8_t *const dst = dsts[i] + 2 * dst_stride + 2;
302 
303     vp9_post_proc_down_and_across(src, dst, src_stride, dst_stride,
304                                   src_height, src_width, ppl);
305   }
306 }
307 
gaussian(double sigma,double mu,double x)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)));
311 }
312 
fillrd(struct postproc_state * state,int q,int a)313 static void fillrd(struct postproc_state *state, int q, int a) {
314   char char_dist[300];
315 
316   double sigma;
317   int ai = a, qi = q, i;
318 
319   vp9_clear_system_state();
320 
321   sigma = ai + .5 + .6 * (63 - qi) / 63.0;
322 
323   /* set up a lookup table of 256 entries that matches
324    * a gaussian distribution with sigma determined by q.
325    */
326   {
327     double i;
328     int next, j;
329 
330     next = 0;
331 
332     for (i = -32; i < 32; i++) {
333       int a = (int)(0.5 + 256 * gaussian(sigma, 0, i));
334 
335       if (a) {
336         for (j = 0; j < a; j++) {
337           char_dist[next + j] = (char) i;
338         }
339 
340         next = next + j;
341       }
342     }
343 
344     for (; next < 256; next++)
345       char_dist[next] = 0;
346   }
347 
348   for (i = 0; i < 3072; i++) {
349     state->noise[i] = char_dist[rand() & 0xff];  // NOLINT
350   }
351 
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];
356   }
357 
358   state->last_q = q;
359   state->last_noise = a;
360 }
361 
vp9_plane_add_noise_c(uint8_t * start,char * noise,char blackclamp[16],char whiteclamp[16],char bothclamp[16],unsigned int width,unsigned int height,int pitch)362 void vp9_plane_add_noise_c(uint8_t *start, char *noise,
363                            char blackclamp[16],
364                            char whiteclamp[16],
365                            char bothclamp[16],
366                            unsigned int width, unsigned int height, int pitch) {
367   unsigned int i, j;
368 
369   for (i = 0; i < height; i++) {
370     uint8_t *pos = start + i * pitch;
371     char  *ref = (char *)(noise + (rand() & 0xff));  // NOLINT
372 
373     for (j = 0; j < width; j++) {
374       if (pos[j] < blackclamp[0])
375         pos[j] = blackclamp[0];
376 
377       if (pos[j] > 255 + whiteclamp[0])
378         pos[j] = 255 + whiteclamp[0];
379 
380       pos[j] += ref[j];
381     }
382   }
383 }
384 
vp9_post_proc_frame(struct VP9Common * cm,YV12_BUFFER_CONFIG * dest,vp9_ppflags_t * ppflags)385 int vp9_post_proc_frame(struct VP9Common *cm,
386                         YV12_BUFFER_CONFIG *dest, vp9_ppflags_t *ppflags) {
387   const int q = MIN(63, cm->lf.filter_level * 10 / 6);
388   const int flags = ppflags->post_proc_flag;
389   YV12_BUFFER_CONFIG *const ppbuf = &cm->post_proc_buffer;
390   struct postproc_state *const ppstate = &cm->postproc_state;
391 
392   if (!cm->frame_to_show)
393     return -1;
394 
395   if (!flags) {
396     *dest = *cm->frame_to_show;
397     return 0;
398   }
399 
400   vp9_clear_system_state();
401 
402 #if CONFIG_VP9_POSTPROC || CONFIG_INTERNAL_STATS
403   if (vp9_realloc_frame_buffer(&cm->post_proc_buffer, cm->width, cm->height,
404                                cm->subsampling_x, cm->subsampling_y,
405                                VP9_DEC_BORDER_IN_PIXELS, NULL, NULL, NULL) < 0)
406     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
407                        "Failed to allocate post-processing buffer");
408 #endif
409 
410   if (flags & VP9D_DEMACROBLOCK) {
411     deblock_and_de_macro_block(cm->frame_to_show, ppbuf,
412                                q + (ppflags->deblocking_level - 5) * 10, 1, 0);
413   } else if (flags & VP9D_DEBLOCK) {
414     vp9_deblock(cm->frame_to_show, ppbuf, q);
415   } else {
416     vp8_yv12_copy_frame(cm->frame_to_show, ppbuf);
417   }
418 
419   if (flags & VP9D_ADDNOISE) {
420     const int noise_level = ppflags->noise_level;
421     if (ppstate->last_q != q ||
422         ppstate->last_noise != noise_level) {
423       fillrd(ppstate, 63 - q, noise_level);
424     }
425 
426     vp9_plane_add_noise(ppbuf->y_buffer, ppstate->noise, ppstate->blackclamp,
427                         ppstate->whiteclamp, ppstate->bothclamp,
428                         ppbuf->y_width, ppbuf->y_height, ppbuf->y_stride);
429   }
430 
431   *dest = *ppbuf;
432 
433   /* handle problem with extending borders */
434   dest->y_width = cm->width;
435   dest->y_height = cm->height;
436   dest->uv_width = dest->y_width >> cm->subsampling_x;
437   dest->uv_height = dest->y_height >> cm->subsampling_y;
438 
439   return 0;
440 }
441 #endif
442