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