14ee2ad04344446e610172a0e73949212923014dfSebastian Redl/*
22cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
32cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor *
42cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor *  Use of this source code is governed by a BSD-style license
52cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor *  that can be found in the LICENSE file in the root of the source
62cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor *  tree. An additional intellectual property rights grant can be found
72cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor *  in the file PATENTS.  All contributing project authors may
82cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor *  be found in the AUTHORS file in the root of the source tree.
92cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor */
10a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redl
112cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include <math.h>
122cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include <stdlib.h>
132cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include <stdio.h>
147faa2ec03a7ef120ac165bb45b6c70a8b20c9f1cSebastian Redl
1589d9980bbc2e4a4ac86673e6ec16fb9f5babb63bDouglas Gregor#include "./vpx_config.h"
160eca89e9890db4d8336ce762a5b359a1d58ca02bArgyrios Kyrtzidis#include "./vpx_scale_rtcd.h"
17e737f5041a36d0befb39ffeed8d50ba15916d3daDouglas Gregor#include "./vp9_rtcd.h"
18e737f5041a36d0befb39ffeed8d50ba15916d3daDouglas Gregor
192cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "vpx_scale/vpx_scale.h"
202cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "vpx_scale/yv12config.h"
212cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
222a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall#include "vp9/common/vp9_onyxc_int.h"
2389eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis#include "vp9/common/vp9_postproc.h"
240b7489194f9f89fac39d57211c1e7953ae50251fDouglas Gregor#include "vp9/common/vp9_systemdependent.h"
257a1fad38256eb4c5129359be85ba1ea1678eb5c9John McCall#include "vp9/common/vp9_textblit.h"
262cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
27a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall#if CONFIG_VP9_POSTPROC
286ab7cd853e9c15cf986a8a7c3db1f8d20e275409Sebastian Redlstatic const short kernel5[] = {
297c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner  1, 1, 4, 1, 1
306a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor};
317c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner
3283d63c78810556d26b62ac4cbae2eda6cdd2570cSteve Naroffconst short vp9_rv[] = {
3314f79002e58556798e86168c63e48d533287eda5Douglas Gregor  8, 5, 2, 2, 8, 12, 4, 9, 8, 3,
3410e286aa8d39fb51a21412850265d9dae74613eeChris Lattner  0, 3, 9, 0, 0, 0, 8, 3, 14, 4,
353251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor  10, 1, 11, 14, 1, 14, 9, 6, 12, 11,
3614f79002e58556798e86168c63e48d533287eda5Douglas Gregor  8, 6, 10, 0, 0, 8, 9, 0, 3, 14,
37bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor  8, 11, 13, 4, 2, 9, 0, 3, 9, 6,
382bec0410d268779f601bd509e0302a500af7ac6aDouglas Gregor  1, 2, 3, 14, 13, 1, 8, 2, 9, 7,
39ab41e63821dc60ad144d0684df8d79a9eef86b75Douglas Gregor  3, 3, 1, 13, 13, 6, 6, 5, 2, 7,
400a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  11, 9, 11, 8, 7, 3, 2, 0, 13, 13,
4117fc223395d51be582fc666bb6ea21bd1dff26dcDouglas Gregor  14, 4, 12, 5, 12, 10, 8, 10, 13, 10,
4217fc223395d51be582fc666bb6ea21bd1dff26dcDouglas Gregor  4, 14, 4, 10, 0, 8, 11, 1, 13, 7,
432596e429a61602312bdd149786045b8a90cd2d10Daniel Dunbar  7, 14, 6, 14, 13, 2, 13, 5, 4, 4,
442cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  0, 10, 0, 5, 13, 2, 12, 7, 11, 13,
45fbfd180495e7800975c6d9bdc6d24e706ef70e34Michael J. Spencer  8, 0, 4, 10, 7, 2, 7, 2, 2, 5,
4614f79002e58556798e86168c63e48d533287eda5Douglas Gregor  3, 4, 7, 3, 3, 14, 14, 5, 9, 13,
4703013fa9a0bf1ef4b907f5fec006c8f4000fdd21Michael J. Spencer  3, 14, 3, 6, 3, 0, 11, 8, 13, 1,
48f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  13, 1, 12, 0, 10, 9, 7, 6, 2, 8,
493c304bd9ec2b4611572d4cbae9e1727bbecb5dc9Chris Lattner  5, 2, 13, 7, 1, 13, 14, 7, 6, 7,
50cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor  9, 6, 10, 11, 7, 8, 7, 5, 14, 8,
51f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  4, 4, 0, 8, 7, 10, 0, 8, 14, 11,
522cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  3, 12, 5, 7, 14, 3, 14, 5, 2, 6,
538538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  11, 12, 12, 8, 0, 11, 13, 1, 2, 0,
542cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  5, 10, 14, 7, 8, 0, 4, 11, 0, 8,
55ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl  0, 3, 10, 5, 8, 0, 11, 6, 7, 8,
565f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  10, 7, 13, 9, 2, 5, 1, 5, 10, 2,
575f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  4, 3, 5, 6, 10, 8, 9, 4, 11, 14,
585f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  0, 10, 0, 5, 13, 2, 12, 7, 11, 13,
596e089c687cc2b914c46859ab7e46fe4c3c6b0afbBenjamin Kramer  8, 0, 4, 10, 7, 2, 7, 2, 2, 5,
60ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl  3, 4, 7, 3, 3, 14, 14, 5, 9, 13,
616e089c687cc2b914c46859ab7e46fe4c3c6b0afbBenjamin Kramer  3, 14, 3, 6, 3, 0, 11, 8, 13, 1,
626e089c687cc2b914c46859ab7e46fe4c3c6b0afbBenjamin Kramer  13, 1, 12, 0, 10, 9, 7, 6, 2, 8,
635f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  5, 2, 13, 7, 1, 13, 14, 7, 6, 7,
645f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  9, 6, 10, 11, 7, 8, 7, 5, 14, 8,
656e089c687cc2b914c46859ab7e46fe4c3c6b0afbBenjamin Kramer  4, 4, 0, 8, 7, 10, 0, 8, 14, 11,
66ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl  3, 12, 5, 7, 14, 3, 14, 5, 2, 6,
67ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl  11, 12, 12, 8, 0, 11, 13, 1, 2, 0,
682cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  5, 10, 14, 7, 8, 0, 4, 11, 0, 8,
692cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  0, 3, 10, 5, 8, 0, 11, 6, 7, 8,
702cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  10, 7, 13, 9, 2, 5, 1, 5, 10, 2,
7112b1c7615d4f9a2edc544be499f895f16ac100edChris Lattner  4, 3, 5, 6, 10, 8, 9, 4, 11, 14,
722cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  3, 8, 3, 7, 8, 5, 11, 4, 12, 3,
733397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl  11, 9, 14, 8, 14, 13, 4, 3, 1, 2,
74a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redl  14, 6, 5, 4, 4, 11, 4, 6, 2, 1,
7589eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis  5, 8, 8, 12, 13, 5, 14, 10, 12, 13,
762cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  0, 9, 5, 5, 11, 10, 13, 9, 10, 13,
772cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor};
782cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
798538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redlvoid vp9_post_proc_down_and_across_c(const uint8_t *src_ptr,
802cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor                                     uint8_t *dst_ptr,
8189eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis                                     int src_pixels_per_line,
828538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl                                     int dst_pixels_per_line,
832cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor                                     int rows,
842cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor                                     int cols,
852cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor                                     int flimit) {
862cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  uint8_t const *p_src;
872cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  uint8_t *p_dst;
882cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  int row;
892cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  int col;
902cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  int i;
912cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  int v;
922cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  int pitch = src_pixels_per_line;
932cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  uint8_t d[8];
943397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl  (void)dst_pixels_per_line;
952cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
962cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  for (row = 0; row < rows; row++) {
972cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    /* post_proc_down for one row */
983397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl    p_src = src_ptr;
992cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    p_dst = dst_ptr;
1008538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl
1012cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    for (col = 0; col < cols; col++) {
1022cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      int kernel = 4;
1033397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl      int v = p_src[col];
1042cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1058538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl      for (i = -2; i <= 2; i++) {
1062cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor        if (abs(v - p_src[col + i * pitch]) > flimit)
1072cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor          goto down_skip_convolve;
1083397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl
1091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        kernel += kernel5[2 + i] * p_src[col + i * pitch];
1108538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl      }
1112cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1122cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      v = (kernel >> 3);
1133397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl    down_skip_convolve:
114df1550fc59b51681d37225934fe4e3acac321621Richard Smith      p_dst[col] = v;
115df1550fc59b51681d37225934fe4e3acac321621Richard Smith    }
1168538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl
1172cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    /* now post_proc_across */
1182cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    p_src = dst_ptr;
1193397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl    p_dst = dst_ptr;
120df1550fc59b51681d37225934fe4e3acac321621Richard Smith
1218538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    for (i = 0; i < 8; i++)
1222cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      d[i] = p_src[i];
1232cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1243397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl    for (col = 0; col < cols; col++) {
1251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      int kernel = 4;
1261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      v = p_src[col];
1278538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl
1282cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      d[col & 7] = v;
1292cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1303397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl      for (i = -2; i <= 2; i++) {
1312cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor        if (abs(v - p_src[col + i]) > flimit)
1322cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor          goto across_skip_convolve;
1330953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
1342cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor        kernel += kernel5[2 + i] * p_src[col + i];
1352cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      }
1363397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl
1372cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      d[col & 7] = (kernel >> 3);
1382cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    across_skip_convolve:
1398538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl
1402cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      if (col >= 2)
1412cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor        p_dst[col - 2] = d[(col - 2) & 7];
1423397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl    }
1432cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1448538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    /* handle the last two pixels */
1452cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    p_dst[col - 2] = d[(col - 2) & 7];
1462cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    p_dst[col - 1] = d[(col - 1) & 7];
1473397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl
1482cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1497e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    /* next row */
1507e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    src_ptr += pitch;
151c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregor    dst_ptr += pitch;
1528538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  }
1532cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
1542cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1553397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlstatic int q2mbl(int x) {
1562cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (x < 20) x = 20;
1572cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
158e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson  x = 50 + (x - 50) * 10 / 8;
1598538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  return x * x / 3;
1602cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
1612cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1623397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlvoid vp9_mbpost_proc_across_ip_c(uint8_t *src, int pitch,
1632cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor                                 int rows, int cols, int flimit) {
1648538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  int r, c, i;
1652cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1662cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  uint8_t *s = src;
1673397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl  uint8_t d[16];
1682cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
169264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola
170264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  for (r = 0; r < rows; r++) {
171a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman    int sumsq = 0;
172425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola    int sum   = 0;
173ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor
174264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola    for (i = -8; i <= 6; i++) {
175f85e193739c953358c865005855253af4f68a497John McCall      sumsq += s[i] * s[i];
1762cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      sum   += s[i];
1772cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      d[i + 8] = 0;
1783397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl    }
1792cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1808538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    for (c = 0; c < cols + 8; c++) {
1812cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      int x = s[c + 7] - s[c - 8];
1822cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      int y = s[c + 7] + s[c - 8];
1833397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl
1842cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      sum  += x;
1852cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      sumsq += x * y;
1862cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1872cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      d[c & 15] = s[c];
1882cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1892cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      if (sumsq * 15 - sum * sum < flimit) {
190c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor        d[c & 15] = (8 + sum + s[c]) >> 4;
19160618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl      }
19260618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl
19360618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl      s[c - 8] = d[(c - 8) & 15];
19460618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl    }
19560618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl
19660618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl    s += pitch;
19760618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  }
19860618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl}
1998538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl
2002cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorvoid vp9_mbpost_proc_down_c(uint8_t *dst, int pitch,
2012cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor                            int rows, int cols, int flimit) {
2023397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl  int r, c, i;
203ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  const short *rv3 = &vp9_rv[63 & rand()]; // NOLINT
2048538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl
205ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  for (c = 0; c < cols; c++) {
206ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    uint8_t *s = &dst[c];
2073397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl    int sumsq = 0;
2082cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    int sum   = 0;
2099763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    uint8_t d[16];
2109763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    const short *rv2 = rv3 + ((c * 17) & 127);
2118538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl
2122cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    for (i = -8; i <= 6; i++) {
2132cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      sumsq += s[i * pitch] * s[i * pitch];
2143397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl      sum   += s[i * pitch];
215c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregor    }
2168538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl
2172cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    for (r = 0; r < rows + 8; r++) {
2182cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      sumsq += s[7 * pitch] * s[ 7 * pitch] - s[-8 * pitch] * s[-8 * pitch];
2193397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl      sum  += s[7 * pitch] - s[-8 * pitch];
2202cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      d[r & 15] = s[0];
2218538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl
2222cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor      if (sumsq * 15 - sum * sum < flimit) {
2232cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor        d[r & 15] = (rv2[r & 127] + sum + s[0]) >> 4;
2243397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl      }
225395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson
2268538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl      s[-8 * pitch] = d[(r - 8) & 15];
227395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson      s += pitch;
228395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson    }
229ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  }
230ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt}
231ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt
232ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Huntstatic void deblock_and_de_macro_block(YV12_BUFFER_CONFIG   *source,
233ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       YV12_BUFFER_CONFIG   *post,
234ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       int                   q,
235ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                       int                   low_var_thresh,
23634b41d939a1328f484511c6002ba2456db879a29Richard Smith                                       int                   flag) {
23734b41d939a1328f484511c6002ba2456db879a29Richard Smith  double level = 6.0e-05 * q * q * q - .0067 * q * q + .306 * q + .0065;
23834b41d939a1328f484511c6002ba2456db879a29Richard Smith  int ppl = (int)(level + .5);
23934b41d939a1328f484511c6002ba2456db879a29Richard Smith  (void) low_var_thresh;
24034b41d939a1328f484511c6002ba2456db879a29Richard Smith  (void) flag;
2413397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl
242be191100e034b23a3e13053757a57b7f5068c24aArgyrios Kyrtzidis  vp9_post_proc_down_and_across(source->y_buffer, post->y_buffer,
2432cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor                                source->y_stride, post->y_stride,
2441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                source->y_height, source->y_width, ppl);
2452cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
2462cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  vp9_mbpost_proc_across_ip(post->y_buffer, post->y_stride, post->y_height,
2472cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor                            post->y_width, q2mbl(q));
2483397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl
2492cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  vp9_mbpost_proc_down(post->y_buffer, post->y_stride, post->y_height,
2508538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl                       post->y_width, q2mbl(q));
2512cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
2522cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  vp9_post_proc_down_and_across(source->u_buffer, post->u_buffer,
2533397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl                                source->uv_stride, post->uv_stride,
2542cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor                                source->uv_height, source->uv_width, ppl);
2558538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  vp9_post_proc_down_and_across(source->v_buffer, post->v_buffer,
2562cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor                                source->uv_stride, post->uv_stride,
2572cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor                                source->uv_height, source->uv_width, ppl);
2589d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall}
2599d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
2609d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCallvoid vp9_deblock(const YV12_BUFFER_CONFIG *src, YV12_BUFFER_CONFIG *dst,
2619d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall                 int q) {
2629d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  const int ppl = (int)(6.0e-05 * q * q * q - 0.0067 * q * q + 0.306 * q
2639d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall                        + 0.0065 + 0.5);
2649d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  int i;
2651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2663397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl  const uint8_t *const srcs[3] = {src->y_buffer, src->u_buffer, src->v_buffer};
26749a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  const int src_strides[3] = {src->y_stride, src->uv_stride, src->uv_stride};
26849a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  const int src_widths[3] = {src->y_width, src->uv_width, src->uv_width};
26949a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  const int src_heights[3] = {src->y_height, src->uv_height, src->uv_height};
2708538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl
27149a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  uint8_t *const dsts[3] = {dst->y_buffer, dst->u_buffer, dst->v_buffer};
27249a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  const int dst_strides[3] = {dst->y_stride, dst->uv_stride, dst->uv_stride};
27349a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
274c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  for (i = 0; i < MAX_MB_PLANE; ++i)
275c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    vp9_post_proc_down_and_across(srcs[i], dsts[i],
276c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                  src_strides[i], dst_strides[i],
277c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                  src_heights[i], src_widths[i], ppl);
278c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor}
279c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor
280c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregorvoid vp9_denoise(const YV12_BUFFER_CONFIG *src, YV12_BUFFER_CONFIG *dst,
281c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                 int q) {
2823397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl  const int ppl = (int)(6.0e-05 * q * q * q - 0.0067 * q * q + 0.306 * q
2832cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor                        + 0.0065 + 0.5);
284be191100e034b23a3e13053757a57b7f5068c24aArgyrios Kyrtzidis  int i;
28590b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis
28690b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  const uint8_t *const srcs[3] = {src->y_buffer, src->u_buffer, src->v_buffer};
28790b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  const int src_strides[3] = {src->y_stride, src->uv_stride, src->uv_stride};
28890b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  const int src_widths[3] = {src->y_width, src->uv_width, src->uv_width};
28990b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  const int src_heights[3] = {src->y_height, src->uv_height, src->uv_height};
2903e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
2913e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  uint8_t *const dsts[3] = {dst->y_buffer, dst->u_buffer, dst->v_buffer};
2929763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  const int dst_strides[3] = {dst->y_stride, dst->uv_stride, dst->uv_stride};
2939763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis
2948538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  for (i = 0; i < MAX_MB_PLANE; ++i) {
29590b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    const int src_stride = src_strides[i];
29690b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    const uint8_t *const src = srcs[i] + 2 * src_stride + 2;
29790b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    const int src_width = src_widths[i] - 4;
2983397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl    const int src_height = src_heights[i] - 4;
299ae8b17f1d5d303af53db5a4f4a375ea6b9356566Argyrios Kyrtzidis
300ae8b17f1d5d303af53db5a4f4a375ea6b9356566Argyrios Kyrtzidis    const int dst_stride = dst_strides[i];
301ae8b17f1d5d303af53db5a4f4a375ea6b9356566Argyrios Kyrtzidis    uint8_t *const dst = dsts[i] + 2 * dst_stride + 2;
3028538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl
30390b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    vp9_post_proc_down_and_across(src, dst, src_stride, dst_stride,
30490b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis                                  src_height, src_width, ppl);
30590b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  }
3063397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl}
30790b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis
30890b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidisstatic double gaussian(double sigma, double mu, double x) {
30990b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  return 1 / (sigma * sqrt(2.0 * 3.14159265)) *
31090b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis         (exp(-(x - mu) * (x - mu) / (2 * sigma * sigma)));
31190b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis}
31290b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis
3133397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redlstatic void fillrd(struct postproc_state *state, int q, int a) {
31490b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  char char_dist[300];
31590b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis
31690b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  double sigma;
3174fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth  int ai = a, qi = q, i;
3188538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl
31990b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  vp9_clear_system_state();
32090b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis
32190b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis  sigma = ai + .5 + .6 * (63 - qi) / 63.0;
3223397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl
3238dfbd8b252ba4e6cf4b7a3422f6ef0ca21312dfeArgyrios Kyrtzidis  /* set up a lookup table of 256 entries that matches
3248dfbd8b252ba4e6cf4b7a3422f6ef0ca21312dfeArgyrios Kyrtzidis   * a gaussian distribution with sigma determined by q.
3258dfbd8b252ba4e6cf4b7a3422f6ef0ca21312dfeArgyrios Kyrtzidis   */
326f48d45e3e36c132bdee3373beec4e8b19ae3f9c4Argyrios Kyrtzidis  {
327f48d45e3e36c132bdee3373beec4e8b19ae3f9c4Argyrios Kyrtzidis    double i;
328f48d45e3e36c132bdee3373beec4e8b19ae3f9c4Argyrios Kyrtzidis    int next, j;
3298538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl
33090b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    next = 0;
33190b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis
33290b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis    for (i = -32; i < 32; i++) {
3333397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl      int a = (int)(0.5 + 256 * gaussian(sigma, 0, i));
33490b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis
3353acad62a239448bef0f5848b2a0d5f7dfefd3d14Argyrios Kyrtzidis      if (a) {
3363acad62a239448bef0f5848b2a0d5f7dfefd3d14Argyrios Kyrtzidis        for (j = 0; j < a; j++) {
3373acad62a239448bef0f5848b2a0d5f7dfefd3d14Argyrios Kyrtzidis          char_dist[next + j] = (char) i;
3383acad62a239448bef0f5848b2a0d5f7dfefd3d14Argyrios Kyrtzidis        }
3393acad62a239448bef0f5848b2a0d5f7dfefd3d14Argyrios Kyrtzidis
3403acad62a239448bef0f5848b2a0d5f7dfefd3d14Argyrios Kyrtzidis        next = next + j;
3413acad62a239448bef0f5848b2a0d5f7dfefd3d14Argyrios Kyrtzidis      }
3428538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    }
3432cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
3442cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    for (; next < 256; next++)
3457536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor      char_dist[next] = 0;
3467536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  }
347cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor
348cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor  for (i = 0; i < 3072; i++) {
349cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor    state->noise[i] = char_dist[rand() & 0xff];  // NOLINT
350cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor  }
3517536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor
3527536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  for (i = 0; i < 16; i++) {
3537536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor    state->blackclamp[i] = -char_dist[0];
354075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    state->whiteclamp[i] = -char_dist[0];
355075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    state->bothclamp[i] = -2 * char_dist[0];
356075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  }
357075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
358075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  state->last_q = q;
3593397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl  state->last_noise = a;
360465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara}
3613acad62a239448bef0f5848b2a0d5f7dfefd3d14Argyrios Kyrtzidis
3623acad62a239448bef0f5848b2a0d5f7dfefd3d14Argyrios Kyrtzidisvoid vp9_plane_add_noise_c(uint8_t *start, char *noise,
3638538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl                           char blackclamp[16],
3642cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor                           char whiteclamp[16],
3652cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor                           char bothclamp[16],
3663397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl                           unsigned int width, unsigned int height, int pitch) {
3673cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  unsigned int i, j;
36831f17ecbef57b5679c017c375db330546b7b5145John McCall
3698538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  // TODO(jbb): why does simd code use both but c doesn't,  normalize and
3703cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  // fix..
3713cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  (void) bothclamp;
3723397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl  for (i = 0; i < height; i++) {
373deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor    uint8_t *pos = start + i * pitch;
3748538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl    char  *ref = (char *)(noise + (rand() & 0xff));  // NOLINT
375c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
376c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    for (j = 0; j < width; j++) {
3773397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl      if (pos[j] < blackclamp[0])
378c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        pos[j] = blackclamp[0];
3792cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
380c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      if (pos[j] > 255 + whiteclamp[0])
381446ee4eb4fc4c705a59365252df7a5c253daafa1Steve Naroff        pos[j] = 255 + whiteclamp[0];
382446ee4eb4fc4c705a59365252df7a5c253daafa1Steve Naroff
3838538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl      pos[j] += ref[j];
3842cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    }
3852cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
386d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff}
3873397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl
3881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpint vp9_post_proc_frame(struct VP9Common *cm,
3898538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl                        YV12_BUFFER_CONFIG *dest, vp9_ppflags_t *ppflags) {
3902cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  const int q = MIN(63, cm->lf.filter_level * 10 / 6);
3912cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  const int flags = ppflags->post_proc_flag;
392a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall  YV12_BUFFER_CONFIG *const ppbuf = &cm->post_proc_buffer;
393a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall  struct postproc_state *const ppstate = &cm->postproc_state;
394a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall
395a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redl  if (!cm->frame_to_show)
39689eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis    return -1;
397a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall
398a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall  if (!flags) {
39989eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis    *dest = *cm->frame_to_show;
400a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall    return 0;
401a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall  }
40251bd803fbdade51d674598ed45da3d54190a656cJohn McCall
403a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall  vp9_clear_system_state();
40451bd803fbdade51d674598ed45da3d54190a656cJohn McCall
405a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall#if CONFIG_VP9_POSTPROC || CONFIG_INTERNAL_STATS
406a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall  if (vp9_realloc_frame_buffer(&cm->post_proc_buffer, cm->width, cm->height,
40751bd803fbdade51d674598ed45da3d54190a656cJohn McCall                               cm->subsampling_x, cm->subsampling_y,
40851bd803fbdade51d674598ed45da3d54190a656cJohn McCall                               VP9_DEC_BORDER_IN_PIXELS, NULL, NULL, NULL) < 0)
409a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
410a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall                       "Failed to allocate post-processing buffer");
411a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall#endif
412a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall
41351bd803fbdade51d674598ed45da3d54190a656cJohn McCall  if (flags & VP9D_DEMACROBLOCK) {
41451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    deblock_and_de_macro_block(cm->frame_to_show, ppbuf,
41551bd803fbdade51d674598ed45da3d54190a656cJohn McCall                               q + (ppflags->deblocking_level - 5) * 10, 1, 0);
41651bd803fbdade51d674598ed45da3d54190a656cJohn McCall  } else if (flags & VP9D_DEBLOCK) {
417ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor    vp9_deblock(cm->frame_to_show, ppbuf, q);
418ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor  } else {
419ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor    vp8_yv12_copy_frame(cm->frame_to_show, ppbuf);
420ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor  }
421ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor
422ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor  if (flags & VP9D_ADDNOISE) {
423ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor    const int noise_level = ppflags->noise_level;
42451bd803fbdade51d674598ed45da3d54190a656cJohn McCall    if (ppstate->last_q != q ||
42551bd803fbdade51d674598ed45da3d54190a656cJohn McCall        ppstate->last_noise != noise_level) {
42651bd803fbdade51d674598ed45da3d54190a656cJohn McCall      fillrd(ppstate, 63 - q, noise_level);
42751bd803fbdade51d674598ed45da3d54190a656cJohn McCall    }
42851bd803fbdade51d674598ed45da3d54190a656cJohn McCall
42951bd803fbdade51d674598ed45da3d54190a656cJohn McCall    vp9_plane_add_noise(ppbuf->y_buffer, ppstate->noise, ppstate->blackclamp,
43051bd803fbdade51d674598ed45da3d54190a656cJohn McCall                        ppstate->whiteclamp, ppstate->bothclamp,
43151bd803fbdade51d674598ed45da3d54190a656cJohn McCall                        ppbuf->y_width, ppbuf->y_height, ppbuf->y_stride);
43251bd803fbdade51d674598ed45da3d54190a656cJohn McCall  }
43351bd803fbdade51d674598ed45da3d54190a656cJohn McCall
43451bd803fbdade51d674598ed45da3d54190a656cJohn McCall  *dest = *ppbuf;
43551bd803fbdade51d674598ed45da3d54190a656cJohn McCall
43651bd803fbdade51d674598ed45da3d54190a656cJohn McCall  /* handle problem with extending borders */
43751bd803fbdade51d674598ed45da3d54190a656cJohn McCall  dest->y_width = cm->width;
43851bd803fbdade51d674598ed45da3d54190a656cJohn McCall  dest->y_height = cm->height;
43951bd803fbdade51d674598ed45da3d54190a656cJohn McCall  dest->uv_width = dest->y_width >> cm->subsampling_x;
44051bd803fbdade51d674598ed45da3d54190a656cJohn McCall  dest->uv_height = dest->y_height >> cm->subsampling_y;
44151bd803fbdade51d674598ed45da3d54190a656cJohn McCall
442b6ab6c1ca733fda2302a1c5066bdfc6218c89e41Abramo Bagnara  return 0;
44351bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
44451bd803fbdade51d674598ed45da3d54190a656cJohn McCall#endif
44551bd803fbdade51d674598ed45da3d54190a656cJohn McCall