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_dsp/vpx_dsp_common.h"
20 #include "vpx_ports/mem.h"
21 #include "vpx_ports/system_state.h"
22 #include "vpx_scale/vpx_scale.h"
23 #include "vpx_scale/yv12config.h"
24 
25 #include "vp9/common/vp9_onyxc_int.h"
26 #include "vp9/common/vp9_postproc.h"
27 #include "vp9/common/vp9_textblit.h"
28 
29 #if CONFIG_VP9_POSTPROC
30 static const int16_t kernel5[] = {
31   1, 1, 4, 1, 1
32 };
33 
34 const int16_t vp9_rv[] = {
35   8, 5, 2, 2, 8, 12, 4, 9, 8, 3,
36   0, 3, 9, 0, 0, 0, 8, 3, 14, 4,
37   10, 1, 11, 14, 1, 14, 9, 6, 12, 11,
38   8, 6, 10, 0, 0, 8, 9, 0, 3, 14,
39   8, 11, 13, 4, 2, 9, 0, 3, 9, 6,
40   1, 2, 3, 14, 13, 1, 8, 2, 9, 7,
41   3, 3, 1, 13, 13, 6, 6, 5, 2, 7,
42   11, 9, 11, 8, 7, 3, 2, 0, 13, 13,
43   14, 4, 12, 5, 12, 10, 8, 10, 13, 10,
44   4, 14, 4, 10, 0, 8, 11, 1, 13, 7,
45   7, 14, 6, 14, 13, 2, 13, 5, 4, 4,
46   0, 10, 0, 5, 13, 2, 12, 7, 11, 13,
47   8, 0, 4, 10, 7, 2, 7, 2, 2, 5,
48   3, 4, 7, 3, 3, 14, 14, 5, 9, 13,
49   3, 14, 3, 6, 3, 0, 11, 8, 13, 1,
50   13, 1, 12, 0, 10, 9, 7, 6, 2, 8,
51   5, 2, 13, 7, 1, 13, 14, 7, 6, 7,
52   9, 6, 10, 11, 7, 8, 7, 5, 14, 8,
53   4, 4, 0, 8, 7, 10, 0, 8, 14, 11,
54   3, 12, 5, 7, 14, 3, 14, 5, 2, 6,
55   11, 12, 12, 8, 0, 11, 13, 1, 2, 0,
56   5, 10, 14, 7, 8, 0, 4, 11, 0, 8,
57   0, 3, 10, 5, 8, 0, 11, 6, 7, 8,
58   10, 7, 13, 9, 2, 5, 1, 5, 10, 2,
59   4, 3, 5, 6, 10, 8, 9, 4, 11, 14,
60   0, 10, 0, 5, 13, 2, 12, 7, 11, 13,
61   8, 0, 4, 10, 7, 2, 7, 2, 2, 5,
62   3, 4, 7, 3, 3, 14, 14, 5, 9, 13,
63   3, 14, 3, 6, 3, 0, 11, 8, 13, 1,
64   13, 1, 12, 0, 10, 9, 7, 6, 2, 8,
65   5, 2, 13, 7, 1, 13, 14, 7, 6, 7,
66   9, 6, 10, 11, 7, 8, 7, 5, 14, 8,
67   4, 4, 0, 8, 7, 10, 0, 8, 14, 11,
68   3, 12, 5, 7, 14, 3, 14, 5, 2, 6,
69   11, 12, 12, 8, 0, 11, 13, 1, 2, 0,
70   5, 10, 14, 7, 8, 0, 4, 11, 0, 8,
71   0, 3, 10, 5, 8, 0, 11, 6, 7, 8,
72   10, 7, 13, 9, 2, 5, 1, 5, 10, 2,
73   4, 3, 5, 6, 10, 8, 9, 4, 11, 14,
74   3, 8, 3, 7, 8, 5, 11, 4, 12, 3,
75   11, 9, 14, 8, 14, 13, 4, 3, 1, 2,
76   14, 6, 5, 4, 4, 11, 4, 6, 2, 1,
77   5, 8, 8, 12, 13, 5, 14, 10, 12, 13,
78   0, 9, 5, 5, 11, 10, 13, 9, 10, 13,
79 };
80 
81 static const uint8_t q_diff_thresh = 20;
82 static const uint8_t last_q_thresh = 170;
83 
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)84 void vp9_post_proc_down_and_across_c(const uint8_t *src_ptr,
85                                      uint8_t *dst_ptr,
86                                      int src_pixels_per_line,
87                                      int dst_pixels_per_line,
88                                      int rows,
89                                      int cols,
90                                      int flimit) {
91   uint8_t const *p_src;
92   uint8_t *p_dst;
93   int row, col, i, v, kernel;
94   int pitch = src_pixels_per_line;
95   uint8_t d[8];
96   (void)dst_pixels_per_line;
97 
98   for (row = 0; row < rows; row++) {
99     /* post_proc_down for one row */
100     p_src = src_ptr;
101     p_dst = dst_ptr;
102 
103     for (col = 0; col < cols; col++) {
104       kernel = 4;
105       v = p_src[col];
106 
107       for (i = -2; i <= 2; i++) {
108         if (abs(v - p_src[col + i * pitch]) > flimit)
109           goto down_skip_convolve;
110 
111         kernel += kernel5[2 + i] * p_src[col + i * pitch];
112       }
113 
114       v = (kernel >> 3);
115     down_skip_convolve:
116       p_dst[col] = v;
117     }
118 
119     /* now post_proc_across */
120     p_src = dst_ptr;
121     p_dst = dst_ptr;
122 
123     for (i = 0; i < 8; i++)
124       d[i] = p_src[i];
125 
126     for (col = 0; col < cols; col++) {
127       kernel = 4;
128       v = p_src[col];
129 
130       d[col & 7] = v;
131 
132       for (i = -2; i <= 2; i++) {
133         if (abs(v - p_src[col + i]) > flimit)
134           goto across_skip_convolve;
135 
136         kernel += kernel5[2 + i] * p_src[col + i];
137       }
138 
139       d[col & 7] = (kernel >> 3);
140     across_skip_convolve:
141 
142       if (col >= 2)
143         p_dst[col - 2] = d[(col - 2) & 7];
144     }
145 
146     /* handle the last two pixels */
147     p_dst[col - 2] = d[(col - 2) & 7];
148     p_dst[col - 1] = d[(col - 1) & 7];
149 
150 
151     /* next row */
152     src_ptr += pitch;
153     dst_ptr += pitch;
154   }
155 }
156 
157 #if CONFIG_VP9_HIGHBITDEPTH
vp9_highbd_post_proc_down_and_across_c(const uint16_t * src_ptr,uint16_t * dst_ptr,int src_pixels_per_line,int dst_pixels_per_line,int rows,int cols,int flimit)158 void vp9_highbd_post_proc_down_and_across_c(const uint16_t *src_ptr,
159                                             uint16_t *dst_ptr,
160                                             int src_pixels_per_line,
161                                             int dst_pixels_per_line,
162                                             int rows,
163                                             int cols,
164                                             int flimit) {
165   uint16_t const *p_src;
166   uint16_t *p_dst;
167   int row, col, i, v, kernel;
168   int pitch = src_pixels_per_line;
169   uint16_t d[8];
170 
171   for (row = 0; row < rows; row++) {
172     // post_proc_down for one row.
173     p_src = src_ptr;
174     p_dst = dst_ptr;
175 
176     for (col = 0; col < cols; col++) {
177       kernel = 4;
178       v = p_src[col];
179 
180       for (i = -2; i <= 2; i++) {
181         if (abs(v - p_src[col + i * pitch]) > flimit)
182           goto down_skip_convolve;
183 
184         kernel += kernel5[2 + i] * p_src[col + i * pitch];
185       }
186 
187       v = (kernel >> 3);
188 
189     down_skip_convolve:
190       p_dst[col] = v;
191     }
192 
193     /* now post_proc_across */
194     p_src = dst_ptr;
195     p_dst = dst_ptr;
196 
197     for (i = 0; i < 8; i++)
198       d[i] = p_src[i];
199 
200     for (col = 0; col < cols; col++) {
201       kernel = 4;
202       v = p_src[col];
203 
204       d[col & 7] = v;
205 
206       for (i = -2; i <= 2; i++) {
207         if (abs(v - p_src[col + i]) > flimit)
208           goto across_skip_convolve;
209 
210         kernel += kernel5[2 + i] * p_src[col + i];
211       }
212 
213       d[col & 7] = (kernel >> 3);
214 
215     across_skip_convolve:
216       if (col >= 2)
217         p_dst[col - 2] = d[(col - 2) & 7];
218     }
219 
220     /* handle the last two pixels */
221     p_dst[col - 2] = d[(col - 2) & 7];
222     p_dst[col - 1] = d[(col - 1) & 7];
223 
224 
225     /* next row */
226     src_ptr += pitch;
227     dst_ptr += dst_pixels_per_line;
228   }
229 }
230 #endif  // CONFIG_VP9_HIGHBITDEPTH
231 
q2mbl(int x)232 static int q2mbl(int x) {
233   if (x < 20) x = 20;
234 
235   x = 50 + (x - 50) * 10 / 8;
236   return x * x / 3;
237 }
238 
vp9_mbpost_proc_across_ip_c(uint8_t * src,int pitch,int rows,int cols,int flimit)239 void vp9_mbpost_proc_across_ip_c(uint8_t *src, int pitch,
240                                  int rows, int cols, int flimit) {
241   int r, c, i;
242   uint8_t *s = src;
243   uint8_t d[16];
244 
245   for (r = 0; r < rows; r++) {
246     int sumsq = 0;
247     int sum = 0;
248 
249     for (i = -8; i <= 6; i++) {
250       sumsq += s[i] * s[i];
251       sum += s[i];
252       d[i + 8] = 0;
253     }
254 
255     for (c = 0; c < cols + 8; c++) {
256       int x = s[c + 7] - s[c - 8];
257       int y = s[c + 7] + s[c - 8];
258 
259       sum += x;
260       sumsq += x * y;
261 
262       d[c & 15] = s[c];
263 
264       if (sumsq * 15 - sum * sum < flimit) {
265         d[c & 15] = (8 + sum + s[c]) >> 4;
266       }
267 
268       s[c - 8] = d[(c - 8) & 15];
269     }
270     s += pitch;
271   }
272 }
273 
274 #if CONFIG_VP9_HIGHBITDEPTH
vp9_highbd_mbpost_proc_across_ip_c(uint16_t * src,int pitch,int rows,int cols,int flimit)275 void vp9_highbd_mbpost_proc_across_ip_c(uint16_t *src, int pitch,
276                                         int rows, int cols, int flimit) {
277   int r, c, i;
278 
279   uint16_t *s = src;
280   uint16_t d[16];
281 
282 
283   for (r = 0; r < rows; r++) {
284     int sumsq = 0;
285     int sum   = 0;
286 
287     for (i = -8; i <= 6; i++) {
288       sumsq += s[i] * s[i];
289       sum   += s[i];
290       d[i + 8] = 0;
291     }
292 
293     for (c = 0; c < cols + 8; c++) {
294       int x = s[c + 7] - s[c - 8];
295       int y = s[c + 7] + s[c - 8];
296 
297       sum  += x;
298       sumsq += x * y;
299 
300       d[c & 15] = s[c];
301 
302       if (sumsq * 15 - sum * sum < flimit) {
303         d[c & 15] = (8 + sum + s[c]) >> 4;
304       }
305 
306       s[c - 8] = d[(c - 8) & 15];
307     }
308 
309     s += pitch;
310   }
311 }
312 #endif  // CONFIG_VP9_HIGHBITDEPTH
313 
vp9_mbpost_proc_down_c(uint8_t * dst,int pitch,int rows,int cols,int flimit)314 void vp9_mbpost_proc_down_c(uint8_t *dst, int pitch,
315                             int rows, int cols, int flimit) {
316   int r, c, i;
317   const short *rv3 = &vp9_rv[63 & rand()]; // NOLINT
318 
319   for (c = 0; c < cols; c++) {
320     uint8_t *s = &dst[c];
321     int sumsq = 0;
322     int sum   = 0;
323     uint8_t d[16];
324     const int16_t *rv2 = rv3 + ((c * 17) & 127);
325 
326     for (i = -8; i <= 6; i++) {
327       sumsq += s[i * pitch] * s[i * pitch];
328       sum   += s[i * pitch];
329     }
330 
331     for (r = 0; r < rows + 8; r++) {
332       sumsq += s[7 * pitch] * s[ 7 * pitch] - s[-8 * pitch] * s[-8 * pitch];
333       sum  += s[7 * pitch] - s[-8 * pitch];
334       d[r & 15] = s[0];
335 
336       if (sumsq * 15 - sum * sum < flimit) {
337         d[r & 15] = (rv2[r & 127] + sum + s[0]) >> 4;
338       }
339 
340       s[-8 * pitch] = d[(r - 8) & 15];
341       s += pitch;
342     }
343   }
344 }
345 
346 #if CONFIG_VP9_HIGHBITDEPTH
vp9_highbd_mbpost_proc_down_c(uint16_t * dst,int pitch,int rows,int cols,int flimit)347 void vp9_highbd_mbpost_proc_down_c(uint16_t *dst, int pitch,
348                                    int rows, int cols, int flimit) {
349   int r, c, i;
350   const int16_t *rv3 = &vp9_rv[63 & rand()];  // NOLINT
351 
352   for (c = 0; c < cols; c++) {
353     uint16_t *s = &dst[c];
354     int sumsq = 0;
355     int sum = 0;
356     uint16_t d[16];
357     const int16_t *rv2 = rv3 + ((c * 17) & 127);
358 
359     for (i = -8; i <= 6; i++) {
360       sumsq += s[i * pitch] * s[i * pitch];
361       sum += s[i * pitch];
362     }
363 
364     for (r = 0; r < rows + 8; r++) {
365       sumsq += s[7 * pitch] * s[ 7 * pitch] - s[-8 * pitch] * s[-8 * pitch];
366       sum += s[7 * pitch] - s[-8 * pitch];
367       d[r & 15] = s[0];
368 
369       if (sumsq * 15 - sum * sum < flimit) {
370         d[r & 15] = (rv2[r & 127] + sum + s[0]) >> 4;
371       }
372 
373       s[-8 * pitch] = d[(r - 8) & 15];
374       s += pitch;
375     }
376   }
377 }
378 #endif  // CONFIG_VP9_HIGHBITDEPTH
379 
deblock_and_de_macro_block(YV12_BUFFER_CONFIG * source,YV12_BUFFER_CONFIG * post,int q,int low_var_thresh,int flag)380 static void deblock_and_de_macro_block(YV12_BUFFER_CONFIG   *source,
381                                        YV12_BUFFER_CONFIG   *post,
382                                        int                   q,
383                                        int                   low_var_thresh,
384                                        int                   flag) {
385   double level = 6.0e-05 * q * q * q - .0067 * q * q + .306 * q + .0065;
386   int ppl = (int)(level + .5);
387   (void) low_var_thresh;
388   (void) flag;
389 
390 #if CONFIG_VP9_HIGHBITDEPTH
391   if (source->flags & YV12_FLAG_HIGHBITDEPTH) {
392     vp9_highbd_post_proc_down_and_across(CONVERT_TO_SHORTPTR(source->y_buffer),
393                                          CONVERT_TO_SHORTPTR(post->y_buffer),
394                                          source->y_stride, post->y_stride,
395                                          source->y_height, source->y_width,
396                                          ppl);
397 
398     vp9_highbd_mbpost_proc_across_ip(CONVERT_TO_SHORTPTR(post->y_buffer),
399                                      post->y_stride, post->y_height,
400                                      post->y_width, q2mbl(q));
401 
402     vp9_highbd_mbpost_proc_down(CONVERT_TO_SHORTPTR(post->y_buffer),
403                                 post->y_stride, post->y_height,
404                                 post->y_width, q2mbl(q));
405 
406     vp9_highbd_post_proc_down_and_across(CONVERT_TO_SHORTPTR(source->u_buffer),
407                                          CONVERT_TO_SHORTPTR(post->u_buffer),
408                                          source->uv_stride, post->uv_stride,
409                                          source->uv_height, source->uv_width,
410                                          ppl);
411     vp9_highbd_post_proc_down_and_across(CONVERT_TO_SHORTPTR(source->v_buffer),
412                                          CONVERT_TO_SHORTPTR(post->v_buffer),
413                                          source->uv_stride, post->uv_stride,
414                                          source->uv_height, source->uv_width,
415                                          ppl);
416   } else {
417     vp9_post_proc_down_and_across(source->y_buffer, post->y_buffer,
418                                   source->y_stride, post->y_stride,
419                                   source->y_height, source->y_width, ppl);
420 
421     vp9_mbpost_proc_across_ip(post->y_buffer, post->y_stride, post->y_height,
422                               post->y_width, q2mbl(q));
423 
424     vp9_mbpost_proc_down(post->y_buffer, post->y_stride, post->y_height,
425                          post->y_width, q2mbl(q));
426 
427     vp9_post_proc_down_and_across(source->u_buffer, post->u_buffer,
428                                   source->uv_stride, post->uv_stride,
429                                   source->uv_height, source->uv_width, ppl);
430     vp9_post_proc_down_and_across(source->v_buffer, post->v_buffer,
431                                   source->uv_stride, post->uv_stride,
432                                   source->uv_height, source->uv_width, ppl);
433   }
434 #else
435   vp9_post_proc_down_and_across(source->y_buffer, post->y_buffer,
436                                 source->y_stride, post->y_stride,
437                                 source->y_height, source->y_width, ppl);
438 
439   vp9_mbpost_proc_across_ip(post->y_buffer, post->y_stride, post->y_height,
440                             post->y_width, q2mbl(q));
441 
442   vp9_mbpost_proc_down(post->y_buffer, post->y_stride, post->y_height,
443                        post->y_width, q2mbl(q));
444 
445   vp9_post_proc_down_and_across(source->u_buffer, post->u_buffer,
446                                 source->uv_stride, post->uv_stride,
447                                 source->uv_height, source->uv_width, ppl);
448   vp9_post_proc_down_and_across(source->v_buffer, post->v_buffer,
449                                 source->uv_stride, post->uv_stride,
450                                 source->uv_height, source->uv_width, ppl);
451 #endif  // CONFIG_VP9_HIGHBITDEPTH
452 }
453 
vp9_deblock(const YV12_BUFFER_CONFIG * src,YV12_BUFFER_CONFIG * dst,int q)454 void vp9_deblock(const YV12_BUFFER_CONFIG *src, YV12_BUFFER_CONFIG *dst,
455                  int q) {
456   const int ppl = (int)(6.0e-05 * q * q * q - 0.0067 * q * q + 0.306 * q
457                         + 0.0065 + 0.5);
458   int i;
459 
460   const uint8_t *const srcs[3] = {src->y_buffer, src->u_buffer, src->v_buffer};
461   const int src_strides[3] = {src->y_stride, src->uv_stride, src->uv_stride};
462   const int src_widths[3] = {src->y_width, src->uv_width, src->uv_width};
463   const int src_heights[3] = {src->y_height, src->uv_height, src->uv_height};
464 
465   uint8_t *const dsts[3] = {dst->y_buffer, dst->u_buffer, dst->v_buffer};
466   const int dst_strides[3] = {dst->y_stride, dst->uv_stride, dst->uv_stride};
467 
468   for (i = 0; i < MAX_MB_PLANE; ++i) {
469 #if CONFIG_VP9_HIGHBITDEPTH
470     assert((src->flags & YV12_FLAG_HIGHBITDEPTH) ==
471            (dst->flags & YV12_FLAG_HIGHBITDEPTH));
472     if (src->flags & YV12_FLAG_HIGHBITDEPTH) {
473       vp9_highbd_post_proc_down_and_across(CONVERT_TO_SHORTPTR(srcs[i]),
474                                            CONVERT_TO_SHORTPTR(dsts[i]),
475                                            src_strides[i], dst_strides[i],
476                                            src_heights[i], src_widths[i], ppl);
477     } else {
478       vp9_post_proc_down_and_across(srcs[i], dsts[i],
479                                     src_strides[i], dst_strides[i],
480                                     src_heights[i], src_widths[i], ppl);
481     }
482 #else
483     vp9_post_proc_down_and_across(srcs[i], dsts[i],
484                                   src_strides[i], dst_strides[i],
485                                   src_heights[i], src_widths[i], ppl);
486 #endif  // CONFIG_VP9_HIGHBITDEPTH
487   }
488 }
489 
vp9_denoise(const YV12_BUFFER_CONFIG * src,YV12_BUFFER_CONFIG * dst,int q)490 void vp9_denoise(const YV12_BUFFER_CONFIG *src, YV12_BUFFER_CONFIG *dst,
491                  int q) {
492   const int ppl = (int)(6.0e-05 * q * q * q - 0.0067 * q * q + 0.306 * q
493                         + 0.0065 + 0.5);
494   int i;
495 
496   const uint8_t *const srcs[3] = {src->y_buffer, src->u_buffer, src->v_buffer};
497   const int src_strides[3] = {src->y_stride, src->uv_stride, src->uv_stride};
498   const int src_widths[3] = {src->y_width, src->uv_width, src->uv_width};
499   const int src_heights[3] = {src->y_height, src->uv_height, src->uv_height};
500 
501   uint8_t *const dsts[3] = {dst->y_buffer, dst->u_buffer, dst->v_buffer};
502   const int dst_strides[3] = {dst->y_stride, dst->uv_stride, dst->uv_stride};
503 
504   for (i = 0; i < MAX_MB_PLANE; ++i) {
505     const int src_stride = src_strides[i];
506     const int src_width = src_widths[i] - 4;
507     const int src_height = src_heights[i] - 4;
508     const int dst_stride = dst_strides[i];
509 
510 #if CONFIG_VP9_HIGHBITDEPTH
511     assert((src->flags & YV12_FLAG_HIGHBITDEPTH) ==
512            (dst->flags & YV12_FLAG_HIGHBITDEPTH));
513     if (src->flags & YV12_FLAG_HIGHBITDEPTH) {
514       const uint16_t *const src_plane = CONVERT_TO_SHORTPTR(
515           srcs[i] + 2 * src_stride + 2);
516       uint16_t *const dst_plane = CONVERT_TO_SHORTPTR(
517           dsts[i] + 2 * dst_stride + 2);
518       vp9_highbd_post_proc_down_and_across(src_plane, dst_plane, src_stride,
519                                            dst_stride, src_height, src_width,
520                                            ppl);
521     } else {
522       const uint8_t *const src_plane = srcs[i] + 2 * src_stride + 2;
523       uint8_t *const dst_plane = dsts[i] + 2 * dst_stride + 2;
524 
525       vp9_post_proc_down_and_across(src_plane, dst_plane, src_stride,
526                                     dst_stride, src_height, src_width, ppl);
527     }
528 #else
529     const uint8_t *const src_plane = srcs[i] + 2 * src_stride + 2;
530     uint8_t *const dst_plane = dsts[i] + 2 * dst_stride + 2;
531     vp9_post_proc_down_and_across(src_plane, dst_plane, src_stride, dst_stride,
532                                   src_height, src_width, ppl);
533 #endif
534   }
535 }
536 
gaussian(double sigma,double mu,double x)537 static double gaussian(double sigma, double mu, double x) {
538   return 1 / (sigma * sqrt(2.0 * 3.14159265)) *
539          (exp(-(x - mu) * (x - mu) / (2 * sigma * sigma)));
540 }
541 
fillrd(struct postproc_state * state,int q,int a)542 static void fillrd(struct postproc_state *state, int q, int a) {
543   char char_dist[300];
544 
545   double sigma;
546   int ai = a, qi = q, i;
547 
548   vpx_clear_system_state();
549 
550   sigma = ai + .5 + .6 * (63 - qi) / 63.0;
551 
552   /* set up a lookup table of 256 entries that matches
553    * a gaussian distribution with sigma determined by q.
554    */
555   {
556     int next, j;
557 
558     next = 0;
559 
560     for (i = -32; i < 32; i++) {
561       int a_i = (int)(0.5 + 256 * gaussian(sigma, 0, i));
562 
563       if (a_i) {
564         for (j = 0; j < a_i; j++) {
565           char_dist[next + j] = (char) i;
566         }
567 
568         next = next + j;
569       }
570     }
571 
572     for (; next < 256; next++)
573       char_dist[next] = 0;
574   }
575 
576   for (i = 0; i < 3072; i++) {
577     state->noise[i] = char_dist[rand() & 0xff];  // NOLINT
578   }
579 
580   for (i = 0; i < 16; i++) {
581     state->blackclamp[i] = -char_dist[0];
582     state->whiteclamp[i] = -char_dist[0];
583     state->bothclamp[i] = -2 * char_dist[0];
584   }
585 
586   state->last_q = q;
587   state->last_noise = a;
588 }
589 
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)590 void vp9_plane_add_noise_c(uint8_t *start, char *noise,
591                            char blackclamp[16],
592                            char whiteclamp[16],
593                            char bothclamp[16],
594                            unsigned int width, unsigned int height, int pitch) {
595   unsigned int i, j;
596 
597   // TODO(jbb): why does simd code use both but c doesn't,  normalize and
598   // fix..
599   (void) bothclamp;
600   for (i = 0; i < height; i++) {
601     uint8_t *pos = start + i * pitch;
602     char  *ref = (char *)(noise + (rand() & 0xff));  // NOLINT
603 
604     for (j = 0; j < width; j++) {
605       if (pos[j] < blackclamp[0])
606         pos[j] = blackclamp[0];
607 
608       if (pos[j] > 255 + whiteclamp[0])
609         pos[j] = 255 + whiteclamp[0];
610 
611       pos[j] += ref[j];
612     }
613   }
614 }
615 
swap_mi_and_prev_mi(VP9_COMMON * cm)616 static void swap_mi_and_prev_mi(VP9_COMMON *cm) {
617   // Current mip will be the prev_mip for the next frame.
618   MODE_INFO *temp = cm->postproc_state.prev_mip;
619   cm->postproc_state.prev_mip = cm->mip;
620   cm->mip = temp;
621 
622   // Update the upper left visible macroblock ptrs.
623   cm->mi = cm->mip + cm->mi_stride + 1;
624   cm->postproc_state.prev_mi = cm->postproc_state.prev_mip + cm->mi_stride + 1;
625 }
626 
vp9_post_proc_frame(struct VP9Common * cm,YV12_BUFFER_CONFIG * dest,vp9_ppflags_t * ppflags)627 int vp9_post_proc_frame(struct VP9Common *cm,
628                         YV12_BUFFER_CONFIG *dest, vp9_ppflags_t *ppflags) {
629   const int q = VPXMIN(105, cm->lf.filter_level * 2);
630   const int flags = ppflags->post_proc_flag;
631   YV12_BUFFER_CONFIG *const ppbuf = &cm->post_proc_buffer;
632   struct postproc_state *const ppstate = &cm->postproc_state;
633 
634   if (!cm->frame_to_show)
635     return -1;
636 
637   if (!flags) {
638     *dest = *cm->frame_to_show;
639     return 0;
640   }
641 
642   vpx_clear_system_state();
643 
644   // Alloc memory for prev_mip in the first frame.
645   if (cm->current_video_frame == 1) {
646     cm->postproc_state.last_base_qindex = cm->base_qindex;
647     cm->postproc_state.last_frame_valid = 1;
648     ppstate->prev_mip = vpx_calloc(cm->mi_alloc_size, sizeof(*cm->mip));
649     if (!ppstate->prev_mip) {
650       return 1;
651     }
652     ppstate->prev_mi = ppstate->prev_mip + cm->mi_stride + 1;
653     memset(ppstate->prev_mip, 0,
654            cm->mi_stride * (cm->mi_rows + 1) * sizeof(*cm->mip));
655   }
656 
657   // Allocate post_proc_buffer_int if needed.
658   if ((flags & VP9D_MFQE) && !cm->post_proc_buffer_int.buffer_alloc) {
659     if ((flags & VP9D_DEMACROBLOCK) || (flags & VP9D_DEBLOCK)) {
660       const int width = ALIGN_POWER_OF_TWO(cm->width, 4);
661       const int height = ALIGN_POWER_OF_TWO(cm->height, 4);
662 
663       if (vpx_alloc_frame_buffer(&cm->post_proc_buffer_int, width, height,
664                                  cm->subsampling_x, cm->subsampling_y,
665 #if CONFIG_VP9_HIGHBITDEPTH
666                                  cm->use_highbitdepth,
667 #endif  // CONFIG_VP9_HIGHBITDEPTH
668                                  VP9_ENC_BORDER_IN_PIXELS,
669                                  cm->byte_alignment) < 0) {
670         vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
671                            "Failed to allocate MFQE framebuffer");
672       }
673 
674       // Ensure that postproc is set to all 0s so that post proc
675       // doesn't pull random data in from edge.
676       memset(cm->post_proc_buffer_int.buffer_alloc, 128,
677              cm->post_proc_buffer.frame_size);
678     }
679   }
680 
681   if (vpx_realloc_frame_buffer(&cm->post_proc_buffer, cm->width, cm->height,
682                                cm->subsampling_x, cm->subsampling_y,
683 #if CONFIG_VP9_HIGHBITDEPTH
684                                cm->use_highbitdepth,
685 #endif
686                                VP9_DEC_BORDER_IN_PIXELS, cm->byte_alignment,
687                                NULL, NULL, NULL) < 0)
688     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
689                        "Failed to allocate post-processing buffer");
690 
691   if ((flags & VP9D_MFQE) && cm->current_video_frame >= 2 &&
692       cm->postproc_state.last_frame_valid && cm->bit_depth == 8 &&
693       cm->postproc_state.last_base_qindex <= last_q_thresh &&
694       cm->base_qindex - cm->postproc_state.last_base_qindex >= q_diff_thresh) {
695     vp9_mfqe(cm);
696     // TODO(jackychen): Consider whether enable deblocking by default
697     // if mfqe is enabled. Need to take both the quality and the speed
698     // into consideration.
699     if ((flags & VP9D_DEMACROBLOCK) || (flags & VP9D_DEBLOCK)) {
700       vp8_yv12_copy_frame(ppbuf, &cm->post_proc_buffer_int);
701     }
702     if ((flags & VP9D_DEMACROBLOCK) && cm->post_proc_buffer_int.buffer_alloc) {
703       deblock_and_de_macro_block(&cm->post_proc_buffer_int, ppbuf,
704                                  q + (ppflags->deblocking_level - 5) * 10,
705                                  1, 0);
706     } else if (flags & VP9D_DEBLOCK) {
707       vp9_deblock(&cm->post_proc_buffer_int, ppbuf, q);
708     } else {
709       vp8_yv12_copy_frame(&cm->post_proc_buffer_int, ppbuf);
710     }
711   } else if (flags & VP9D_DEMACROBLOCK) {
712     deblock_and_de_macro_block(cm->frame_to_show, ppbuf,
713                                q + (ppflags->deblocking_level - 5) * 10, 1, 0);
714   } else if (flags & VP9D_DEBLOCK) {
715     vp9_deblock(cm->frame_to_show, ppbuf, q);
716   } else {
717     vp8_yv12_copy_frame(cm->frame_to_show, ppbuf);
718   }
719 
720   cm->postproc_state.last_base_qindex = cm->base_qindex;
721   cm->postproc_state.last_frame_valid = 1;
722 
723   if (flags & VP9D_ADDNOISE) {
724     const int noise_level = ppflags->noise_level;
725     if (ppstate->last_q != q ||
726         ppstate->last_noise != noise_level) {
727       fillrd(ppstate, 63 - q, noise_level);
728     }
729 
730     vp9_plane_add_noise(ppbuf->y_buffer, ppstate->noise, ppstate->blackclamp,
731                         ppstate->whiteclamp, ppstate->bothclamp,
732                         ppbuf->y_width, ppbuf->y_height, ppbuf->y_stride);
733   }
734 
735   *dest = *ppbuf;
736 
737   /* handle problem with extending borders */
738   dest->y_width = cm->width;
739   dest->y_height = cm->height;
740   dest->uv_width = dest->y_width >> cm->subsampling_x;
741   dest->uv_height = dest->y_height >> cm->subsampling_y;
742 
743   swap_mi_and_prev_mi(cm);
744   return 0;
745 }
746 #endif  // CONFIG_VP9_POSTPROC
747