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