Update To 11.40.268.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 #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 void vp9_post_proc_down_and_across_c(const uint8_t *src_ptr,
83                                      uint8_t *dst_ptr,
84                                      int src_pixels_per_line,
85                                      int dst_pixels_per_line,
86                                      int rows,
87                                      int cols,
88                                      int flimit) {
89   uint8_t const *p_src;
90   uint8_t *p_dst;
91   int row;
92   int col;
93   int i;
94   int v;
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       int kernel = 4;
106       int 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       int 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;
169   int col;
170   int i;
171   int v;
172   int pitch = src_pixels_per_line;
173   uint16_t d[8];
174
175   for (row = 0; row < rows; row++) {
176     // post_proc_down for one row.
177     p_src = src_ptr;
178     p_dst = dst_ptr;
179
180     for (col = 0; col < cols; col++) {
181       int kernel = 4;
182       int v = p_src[col];
183
184       for (i = -2; i <= 2; i++) {
185         if (abs(v - p_src[col + i * pitch]) > flimit)
186           goto down_skip_convolve;
187
188         kernel += kernel5[2 + i] * p_src[col + i * pitch];
189       }
190
191       v = (kernel >> 3);
192
193     down_skip_convolve:
194       p_dst[col] = v;
195     }
196
197     /* now post_proc_across */
198     p_src = dst_ptr;
199     p_dst = dst_ptr;
200
201     for (i = 0; i < 8; i++)
202       d[i] = p_src[i];
203
204     for (col = 0; col < cols; col++) {
205       int kernel = 4;
206       v = p_src[col];
207
208       d[col & 7] = v;
209
210       for (i = -2; i <= 2; i++) {
211         if (abs(v - p_src[col + i]) > flimit)
212           goto across_skip_convolve;
213
214         kernel += kernel5[2 + i] * p_src[col + i];
215       }
216
217       d[col & 7] = (kernel >> 3);
218
219     across_skip_convolve:
220       if (col >= 2)
221         p_dst[col - 2] = d[(col - 2) & 7];
222     }
223
224     /* handle the last two pixels */
225     p_dst[col - 2] = d[(col - 2) & 7];
226     p_dst[col - 1] = d[(col - 1) & 7];
227
228
229     /* next row */
230     src_ptr += pitch;
231     dst_ptr += dst_pixels_per_line;
232   }
233 }
234 #endif  // CONFIG_VP9_HIGHBITDEPTH
235
236 static int q2mbl(int x) {
237   if (x < 20) x = 20;
238
239   x = 50 + (x - 50) * 10 / 8;
240   return x * x / 3;
241 }
242
243 void vp9_mbpost_proc_across_ip_c(uint8_t *src, int pitch,
244                                  int rows, int cols, int flimit) {
245   int r, c, i;
246   uint8_t *s = src;
247   uint8_t d[16];
248
249   for (r = 0; r < rows; r++) {
250     int sumsq = 0;
251     int sum = 0;
252
253     for (i = -8; i <= 6; i++) {
254       sumsq += s[i] * s[i];
255       sum += s[i];
256       d[i + 8] = 0;
257     }
258
259     for (c = 0; c < cols + 8; c++) {
260       int x = s[c + 7] - s[c - 8];
261       int y = s[c + 7] + s[c - 8];
262
263       sum += x;
264       sumsq += x * y;
265
266       d[c & 15] = s[c];
267
268       if (sumsq * 15 - sum * sum < flimit) {
269         d[c & 15] = (8 + sum + s[c]) >> 4;
270       }
271
272       s[c - 8] = d[(c - 8) & 15];
273     }
274     s += pitch;
275   }
276 }
277
278 #if CONFIG_VP9_HIGHBITDEPTH
279 void vp9_highbd_mbpost_proc_across_ip_c(uint16_t *src, int pitch,
280                                         int rows, int cols, int flimit) {
281   int r, c, i;
282
283   uint16_t *s = src;
284   uint16_t d[16];
285
286
287   for (r = 0; r < rows; r++) {
288     int sumsq = 0;
289     int sum   = 0;
290
291     for (i = -8; i <= 6; i++) {
292       sumsq += s[i] * s[i];
293       sum   += s[i];
294       d[i + 8] = 0;
295     }
296
297     for (c = 0; c < cols + 8; c++) {
298       int x = s[c + 7] - s[c - 8];
299       int y = s[c + 7] + s[c - 8];
300
301       sum  += x;
302       sumsq += x * y;
303
304       d[c & 15] = s[c];
305
306       if (sumsq * 15 - sum * sum < flimit) {
307         d[c & 15] = (8 + sum + s[c]) >> 4;
308       }
309
310       s[c - 8] = d[(c - 8) & 15];
311     }
312
313     s += pitch;
314   }
315 }
316 #endif  // CONFIG_VP9_HIGHBITDEPTH
317
318 void vp9_mbpost_proc_down_c(uint8_t *dst, int pitch,
319                             int rows, int cols, int flimit) {
320   int r, c, i;
321   const short *rv3 = &vp9_rv[63 & rand()]; // NOLINT
322
323   for (c = 0; c < cols; c++) {
324     uint8_t *s = &dst[c];
325     int sumsq = 0;
326     int sum   = 0;
327     uint8_t d[16];
328     const short *rv2 = rv3 + ((c * 17) & 127);
329
330     for (i = -8; i <= 6; i++) {
331       sumsq += s[i * pitch] * s[i * pitch];
332       sum   += s[i * pitch];
333     }
334
335     for (r = 0; r < rows + 8; r++) {
336       sumsq += s[7 * pitch] * s[ 7 * pitch] - s[-8 * pitch] * s[-8 * pitch];
337       sum  += s[7 * pitch] - s[-8 * pitch];
338       d[r & 15] = s[0];
339
340       if (sumsq * 15 - sum * sum < flimit) {
341         d[r & 15] = (rv2[r & 127] + sum + s[0]) >> 4;
342       }
343
344       s[-8 * pitch] = d[(r - 8) & 15];
345       s += pitch;
346     }
347   }
348 }
349
350 #if CONFIG_VP9_HIGHBITDEPTH
351 void vp9_highbd_mbpost_proc_down_c(uint16_t *dst, int pitch,
352                                    int rows, int cols, int flimit) {
353   int r, c, i;
354   const int16_t *rv3 = &vp9_rv[63 & rand()];  // NOLINT
355
356   for (c = 0; c < cols; c++) {
357     uint16_t *s = &dst[c];
358     int sumsq = 0;
359     int sum = 0;
360     uint16_t d[16];
361     const int16_t *rv2 = rv3 + ((c * 17) & 127);
362
363     for (i = -8; i <= 6; i++) {
364       sumsq += s[i * pitch] * s[i * pitch];
365       sum += s[i * pitch];
366     }
367
368     for (r = 0; r < rows + 8; r++) {
369       sumsq += s[7 * pitch] * s[ 7 * pitch] - s[-8 * pitch] * s[-8 * pitch];
370       sum += s[7 * pitch] - s[-8 * pitch];
371       d[r & 15] = s[0];
372
373       if (sumsq * 15 - sum * sum < flimit) {
374         d[r & 15] = (rv2[r & 127] + sum + s[0]) >> 4;
375       }
376
377       s[-8 * pitch] = d[(r - 8) & 15];
378       s += pitch;
379     }
380   }
381 }
382 #endif  // CONFIG_VP9_HIGHBITDEPTH
383
384 static void deblock_and_de_macro_block(YV12_BUFFER_CONFIG   *source,
385                                        YV12_BUFFER_CONFIG   *post,
386                                        int                   q,
387                                        int                   low_var_thresh,
388                                        int                   flag) {
389   double level = 6.0e-05 * q * q * q - .0067 * q * q + .306 * q + .0065;
390   int ppl = (int)(level + .5);
391   (void) low_var_thresh;
392   (void) flag;
393
394 #if CONFIG_VP9_HIGHBITDEPTH
395   if (source->flags & YV12_FLAG_HIGHBITDEPTH) {
396     vp9_highbd_post_proc_down_and_across(CONVERT_TO_SHORTPTR(source->y_buffer),
397                                          CONVERT_TO_SHORTPTR(post->y_buffer),
398                                          source->y_stride, post->y_stride,
399                                          source->y_height, source->y_width,
400                                          ppl);
401
402     vp9_highbd_mbpost_proc_across_ip(CONVERT_TO_SHORTPTR(post->y_buffer),
403                                      post->y_stride, post->y_height,
404                                      post->y_width, q2mbl(q));
405
406     vp9_highbd_mbpost_proc_down(CONVERT_TO_SHORTPTR(post->y_buffer),
407                                 post->y_stride, post->y_height,
408                                 post->y_width, q2mbl(q));
409
410     vp9_highbd_post_proc_down_and_across(CONVERT_TO_SHORTPTR(source->u_buffer),
411                                          CONVERT_TO_SHORTPTR(post->u_buffer),
412                                          source->uv_stride, post->uv_stride,
413                                          source->uv_height, source->uv_width,
414                                          ppl);
415     vp9_highbd_post_proc_down_and_across(CONVERT_TO_SHORTPTR(source->v_buffer),
416                                          CONVERT_TO_SHORTPTR(post->v_buffer),
417                                          source->uv_stride, post->uv_stride,
418                                          source->uv_height, source->uv_width,
419                                          ppl);
420   } else {
421     vp9_post_proc_down_and_across(source->y_buffer, post->y_buffer,
422                                   source->y_stride, post->y_stride,
423                                   source->y_height, source->y_width, ppl);
424
425     vp9_mbpost_proc_across_ip(post->y_buffer, post->y_stride, post->y_height,
426                               post->y_width, q2mbl(q));
427
428     vp9_mbpost_proc_down(post->y_buffer, post->y_stride, post->y_height,
429                          post->y_width, q2mbl(q));
430
431     vp9_post_proc_down_and_across(source->u_buffer, post->u_buffer,
432                                   source->uv_stride, post->uv_stride,
433                                   source->uv_height, source->uv_width, ppl);
434     vp9_post_proc_down_and_across(source->v_buffer, post->v_buffer,
435                                   source->uv_stride, post->uv_stride,
436                                   source->uv_height, source->uv_width, ppl);
437   }
438 #else
439   vp9_post_proc_down_and_across(source->y_buffer, post->y_buffer,
440                                 source->y_stride, post->y_stride,
441                                 source->y_height, source->y_width, ppl);
442
443   vp9_mbpost_proc_across_ip(post->y_buffer, post->y_stride, post->y_height,
444                             post->y_width, q2mbl(q));
445
446   vp9_mbpost_proc_down(post->y_buffer, post->y_stride, post->y_height,
447                        post->y_width, q2mbl(q));
448
449   vp9_post_proc_down_and_across(source->u_buffer, post->u_buffer,
450                                 source->uv_stride, post->uv_stride,
451                                 source->uv_height, source->uv_width, ppl);
452   vp9_post_proc_down_and_across(source->v_buffer, post->v_buffer,
453                                 source->uv_stride, post->uv_stride,
454                                 source->uv_height, source->uv_width, ppl);
455 #endif  // CONFIG_VP9_HIGHBITDEPTH
456 }
457
458 void vp9_deblock(const YV12_BUFFER_CONFIG *src, YV12_BUFFER_CONFIG *dst,
459                  int q) {
460   const int ppl = (int)(6.0e-05 * q * q * q - 0.0067 * q * q + 0.306 * q
461                         + 0.0065 + 0.5);
462   int i;
463
464   const uint8_t *const srcs[3] = {src->y_buffer, src->u_buffer, src->v_buffer};
465   const int src_strides[3] = {src->y_stride, src->uv_stride, src->uv_stride};
466   const int src_widths[3] = {src->y_width, src->uv_width, src->uv_width};
467   const int src_heights[3] = {src->y_height, src->uv_height, src->uv_height};
468
469   uint8_t *const dsts[3] = {dst->y_buffer, dst->u_buffer, dst->v_buffer};
470   const int dst_strides[3] = {dst->y_stride, dst->uv_stride, dst->uv_stride};
471
472   for (i = 0; i < MAX_MB_PLANE; ++i) {
473 #if CONFIG_VP9_HIGHBITDEPTH
474     assert((src->flags & YV12_FLAG_HIGHBITDEPTH) ==
475            (dst->flags & YV12_FLAG_HIGHBITDEPTH));
476     if (src->flags & YV12_FLAG_HIGHBITDEPTH) {
477       vp9_highbd_post_proc_down_and_across(CONVERT_TO_SHORTPTR(srcs[i]),
478                                            CONVERT_TO_SHORTPTR(dsts[i]),
479                                            src_strides[i], dst_strides[i],
480                                            src_heights[i], src_widths[i], ppl);
481     } else {
482       vp9_post_proc_down_and_across(srcs[i], dsts[i],
483                                     src_strides[i], dst_strides[i],
484                                     src_heights[i], src_widths[i], ppl);
485     }
486 #else
487     vp9_post_proc_down_and_across(srcs[i], dsts[i],
488                                   src_strides[i], dst_strides[i],
489                                   src_heights[i], src_widths[i], ppl);
490 #endif  // CONFIG_VP9_HIGHBITDEPTH
491   }
492 }
493
494 void vp9_denoise(const YV12_BUFFER_CONFIG *src, YV12_BUFFER_CONFIG *dst,
495                  int q) {
496   const int ppl = (int)(6.0e-05 * q * q * q - 0.0067 * q * q + 0.306 * q
497                         + 0.0065 + 0.5);
498   int i;
499
500   const uint8_t *const srcs[3] = {src->y_buffer, src->u_buffer, src->v_buffer};
501   const int src_strides[3] = {src->y_stride, src->uv_stride, src->uv_stride};
502   const int src_widths[3] = {src->y_width, src->uv_width, src->uv_width};
503   const int src_heights[3] = {src->y_height, src->uv_height, src->uv_height};
504
505   uint8_t *const dsts[3] = {dst->y_buffer, dst->u_buffer, dst->v_buffer};
506   const int dst_strides[3] = {dst->y_stride, dst->uv_stride, dst->uv_stride};
507
508   for (i = 0; i < MAX_MB_PLANE; ++i) {
509     const int src_stride = src_strides[i];
510     const int src_width = src_widths[i] - 4;
511     const int src_height = src_heights[i] - 4;
512     const int dst_stride = dst_strides[i];
513
514 #if CONFIG_VP9_HIGHBITDEPTH
515     assert((src->flags & YV12_FLAG_HIGHBITDEPTH) ==
516            (dst->flags & YV12_FLAG_HIGHBITDEPTH));
517     if (src->flags & YV12_FLAG_HIGHBITDEPTH) {
518       const uint16_t *const src = CONVERT_TO_SHORTPTR(srcs[i] + 2 * src_stride
519                                                       + 2);
520       uint16_t *const dst = CONVERT_TO_SHORTPTR(dsts[i] + 2 * dst_stride + 2);
521       vp9_highbd_post_proc_down_and_across(src, dst, src_stride, dst_stride,
522                                            src_height, src_width, ppl);
523     } else {
524       const uint8_t *const src = srcs[i] + 2 * src_stride + 2;
525       uint8_t *const dst = dsts[i] + 2 * dst_stride + 2;
526
527       vp9_post_proc_down_and_across(src, dst, src_stride, dst_stride,
528                                     src_height, src_width, ppl);
529     }
530 #else
531     const uint8_t *const src = srcs[i] + 2 * src_stride + 2;
532     uint8_t *const dst = dsts[i] + 2 * dst_stride + 2;
533     vp9_post_proc_down_and_across(src, dst, src_stride, dst_stride,
534                                   src_height, src_width, ppl);
535 #endif
536   }
537 }
538
539 static double gaussian(double sigma, double mu, double x) {
540   return 1 / (sigma * sqrt(2.0 * 3.14159265)) *
541          (exp(-(x - mu) * (x - mu) / (2 * sigma * sigma)));
542 }
543
544 static void fillrd(struct postproc_state *state, int q, int a) {
545   char char_dist[300];
546
547   double sigma;
548   int ai = a, qi = q, i;
549
550   vp9_clear_system_state();
551
552   sigma = ai + .5 + .6 * (63 - qi) / 63.0;
553
554   /* set up a lookup table of 256 entries that matches
555    * a gaussian distribution with sigma determined by q.
556    */
557   {
558     double i;
559     int next, j;
560
561     next = 0;
562
563     for (i = -32; i < 32; i++) {
564       int a = (int)(0.5 + 256 * gaussian(sigma, 0, i));
565
566       if (a) {
567         for (j = 0; j < a; j++) {
568           char_dist[next + j] = (char) i;
569         }
570
571         next = next + j;
572       }
573     }
574
575     for (; next < 256; next++)
576       char_dist[next] = 0;
577   }
578
579   for (i = 0; i < 3072; i++) {
580     state->noise[i] = char_dist[rand() & 0xff];  // NOLINT
581   }
582
583   for (i = 0; i < 16; i++) {
584     state->blackclamp[i] = -char_dist[0];
585     state->whiteclamp[i] = -char_dist[0];
586     state->bothclamp[i] = -2 * char_dist[0];
587   }
588
589   state->last_q = q;
590   state->last_noise = a;
591 }
592
593 void vp9_plane_add_noise_c(uint8_t *start, char *noise,
594                            char blackclamp[16],
595                            char whiteclamp[16],
596                            char bothclamp[16],
597                            unsigned int width, unsigned int height, int pitch) {
598   unsigned int i, j;
599
600   // TODO(jbb): why does simd code use both but c doesn't,  normalize and
601   // fix..
602   (void) bothclamp;
603   for (i = 0; i < height; i++) {
604     uint8_t *pos = start + i * pitch;
605     char  *ref = (char *)(noise + (rand() & 0xff));  // NOLINT
606
607     for (j = 0; j < width; j++) {
608       if (pos[j] < blackclamp[0])
609         pos[j] = blackclamp[0];
610
611       if (pos[j] > 255 + whiteclamp[0])
612         pos[j] = 255 + whiteclamp[0];
613
614       pos[j] += ref[j];
615     }
616   }
617 }
618
619 int vp9_post_proc_frame(struct VP9Common *cm,
620                         YV12_BUFFER_CONFIG *dest, vp9_ppflags_t *ppflags) {
621   const int q = MIN(63, cm->lf.filter_level * 10 / 6);
622   const int flags = ppflags->post_proc_flag;
623   YV12_BUFFER_CONFIG *const ppbuf = &cm->post_proc_buffer;
624   struct postproc_state *const ppstate = &cm->postproc_state;
625
626   if (!cm->frame_to_show)
627     return -1;
628
629   if (!flags) {
630     *dest = *cm->frame_to_show;
631     return 0;
632   }
633
634   vp9_clear_system_state();
635
636 #if CONFIG_VP9_POSTPROC || CONFIG_INTERNAL_STATS
637   if (vp9_realloc_frame_buffer(&cm->post_proc_buffer, cm->width, cm->height,
638                                cm->subsampling_x, cm->subsampling_y,
639 #if CONFIG_VP9_HIGHBITDEPTH
640                                cm->use_highbitdepth,
641 #endif
642                                VP9_DEC_BORDER_IN_PIXELS, NULL, NULL, NULL) < 0)
643     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
644                        "Failed to allocate post-processing buffer");
645 #endif
646
647   if (flags & VP9D_DEMACROBLOCK) {
648     deblock_and_de_macro_block(cm->frame_to_show, ppbuf,
649                                q + (ppflags->deblocking_level - 5) * 10, 1, 0);
650   } else if (flags & VP9D_DEBLOCK) {
651     vp9_deblock(cm->frame_to_show, ppbuf, q);
652   } else {
653     vp8_yv12_copy_frame(cm->frame_to_show, ppbuf);
654   }
655
656   if (flags & VP9D_ADDNOISE) {
657     const int noise_level = ppflags->noise_level;
658     if (ppstate->last_q != q ||
659         ppstate->last_noise != noise_level) {
660       fillrd(ppstate, 63 - q, noise_level);
661     }
662
663     vp9_plane_add_noise(ppbuf->y_buffer, ppstate->noise, ppstate->blackclamp,
664                         ppstate->whiteclamp, ppstate->bothclamp,
665                         ppbuf->y_width, ppbuf->y_height, ppbuf->y_stride);
666   }
667
668   *dest = *ppbuf;
669
670   /* handle problem with extending borders */
671   dest->y_width = cm->width;
672   dest->y_height = cm->height;
673   dest->uv_width = dest->y_width >> cm->subsampling_x;
674   dest->uv_height = dest->y_height >> cm->subsampling_y;
675
676   return 0;
677 }
678 #endif