Upstream version 8.37.180.0
[platform/framework/web/crosswalk.git] / src / third_party / libvpx / source / libvpx / vp9 / common / vp9_postproc.c
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
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
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
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
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
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
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[4] = {src->y_buffer, src->u_buffer, src->v_buffer,
267                                   src->alpha_buffer};
268   const int src_strides[4] = {src->y_stride, src->uv_stride, src->uv_stride,
269                               src->alpha_stride};
270   const int src_widths[4] = {src->y_width, src->uv_width, src->uv_width,
271                              src->alpha_width};
272   const int src_heights[4] = {src->y_height, src->uv_height, src->uv_height,
273                               src->alpha_height};
274
275   uint8_t *const dsts[4] = {dst->y_buffer, dst->u_buffer, dst->v_buffer,
276                             dst->alpha_buffer};
277   const int dst_strides[4] = {dst->y_stride, dst->uv_stride, dst->uv_stride,
278                               dst->alpha_stride};
279
280   for (i = 0; i < MAX_MB_PLANE; ++i)
281     vp9_post_proc_down_and_across(srcs[i], dsts[i],
282                                   src_strides[i], dst_strides[i],
283                                   src_heights[i], src_widths[i], ppl);
284 }
285
286 void vp9_denoise(const YV12_BUFFER_CONFIG *src, YV12_BUFFER_CONFIG *dst,
287                  int q) {
288   const int ppl = (int)(6.0e-05 * q * q * q - 0.0067 * q * q + 0.306 * q
289                         + 0.0065 + 0.5);
290   int i;
291
292   const uint8_t *const srcs[4] = {src->y_buffer, src->u_buffer, src->v_buffer,
293                                   src->alpha_buffer};
294   const int src_strides[4] = {src->y_stride, src->uv_stride, src->uv_stride,
295                               src->alpha_stride};
296   const int src_widths[4] = {src->y_width, src->uv_width, src->uv_width,
297                              src->alpha_width};
298   const int src_heights[4] = {src->y_height, src->uv_height, src->uv_height,
299                               src->alpha_height};
300
301   uint8_t *const dsts[4] = {dst->y_buffer, dst->u_buffer, dst->v_buffer,
302                             dst->alpha_buffer};
303   const int dst_strides[4] = {dst->y_stride, dst->uv_stride, dst->uv_stride,
304                               dst->alpha_stride};
305
306   for (i = 0; i < MAX_MB_PLANE; ++i) {
307     const int src_stride = src_strides[i];
308     const uint8_t *const src = srcs[i] + 2 * src_stride + 2;
309     const int src_width = src_widths[i] - 4;
310     const int src_height = src_heights[i] - 4;
311
312     const int dst_stride = dst_strides[i];
313     uint8_t *const dst = dsts[i] + 2 * dst_stride + 2;
314
315     vp9_post_proc_down_and_across(src, dst, src_stride, dst_stride,
316                                   src_height, src_width, ppl);
317   }
318 }
319
320 static double gaussian(double sigma, double mu, double x) {
321   return 1 / (sigma * sqrt(2.0 * 3.14159265)) *
322          (exp(-(x - mu) * (x - mu) / (2 * sigma * sigma)));
323 }
324
325 static void fillrd(struct postproc_state *state, int q, int a) {
326   char char_dist[300];
327
328   double sigma;
329   int ai = a, qi = q, i;
330
331   vp9_clear_system_state();
332
333   sigma = ai + .5 + .6 * (63 - qi) / 63.0;
334
335   /* set up a lookup table of 256 entries that matches
336    * a gaussian distribution with sigma determined by q.
337    */
338   {
339     double i;
340     int next, j;
341
342     next = 0;
343
344     for (i = -32; i < 32; i++) {
345       int a = (int)(0.5 + 256 * gaussian(sigma, 0, i));
346
347       if (a) {
348         for (j = 0; j < a; j++) {
349           char_dist[next + j] = (char) i;
350         }
351
352         next = next + j;
353       }
354     }
355
356     for (; next < 256; next++)
357       char_dist[next] = 0;
358   }
359
360   for (i = 0; i < 3072; i++) {
361     state->noise[i] = char_dist[rand() & 0xff];  // NOLINT
362   }
363
364   for (i = 0; i < 16; i++) {
365     state->blackclamp[i] = -char_dist[0];
366     state->whiteclamp[i] = -char_dist[0];
367     state->bothclamp[i] = -2 * char_dist[0];
368   }
369
370   state->last_q = q;
371   state->last_noise = a;
372 }
373
374 void vp9_plane_add_noise_c(uint8_t *start, char *noise,
375                            char blackclamp[16],
376                            char whiteclamp[16],
377                            char bothclamp[16],
378                            unsigned int width, unsigned int height, int pitch) {
379   unsigned int i, j;
380
381   for (i = 0; i < height; i++) {
382     uint8_t *pos = start + i * pitch;
383     char  *ref = (char *)(noise + (rand() & 0xff));  // NOLINT
384
385     for (j = 0; j < width; j++) {
386       if (pos[j] < blackclamp[0])
387         pos[j] = blackclamp[0];
388
389       if (pos[j] > 255 + whiteclamp[0])
390         pos[j] = 255 + whiteclamp[0];
391
392       pos[j] += ref[j];
393     }
394   }
395 }
396
397 int vp9_post_proc_frame(struct VP9Common *cm,
398                         YV12_BUFFER_CONFIG *dest, vp9_ppflags_t *ppflags) {
399   const int q = MIN(63, cm->lf.filter_level * 10 / 6);
400   const int flags = ppflags->post_proc_flag;
401   YV12_BUFFER_CONFIG *const ppbuf = &cm->post_proc_buffer;
402   struct postproc_state *const ppstate = &cm->postproc_state;
403
404   if (!cm->frame_to_show)
405     return -1;
406
407   if (!flags) {
408     *dest = *cm->frame_to_show;
409     return 0;
410   }
411
412   vp9_clear_system_state();
413
414   if (flags & VP9D_DEMACROBLOCK) {
415     deblock_and_de_macro_block(cm->frame_to_show, ppbuf,
416                                q + (ppflags->deblocking_level - 5) * 10, 1, 0);
417   } else if (flags & VP9D_DEBLOCK) {
418     vp9_deblock(cm->frame_to_show, ppbuf, q);
419   } else {
420     vp8_yv12_copy_frame(cm->frame_to_show, ppbuf);
421   }
422
423   if (flags & VP9D_ADDNOISE) {
424     const int noise_level = ppflags->noise_level;
425     if (ppstate->last_q != q ||
426         ppstate->last_noise != noise_level) {
427       fillrd(ppstate, 63 - q, noise_level);
428     }
429
430     vp9_plane_add_noise(ppbuf->y_buffer, ppstate->noise, ppstate->blackclamp,
431                         ppstate->whiteclamp, ppstate->bothclamp,
432                         ppbuf->y_width, ppbuf->y_height, ppbuf->y_stride);
433   }
434
435   *dest = *ppbuf;
436
437   /* handle problem with extending borders */
438   dest->y_width = cm->width;
439   dest->y_height = cm->height;
440   dest->uv_width = dest->y_width >> cm->subsampling_x;
441   dest->uv_height = dest->y_height >> cm->subsampling_y;
442
443   return 0;
444 }
445 #endif