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