vp9_postproc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. #include <math.h>
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include "./vpx_dsp_rtcd.h"
  14. #include "./vpx_config.h"
  15. #include "./vpx_scale_rtcd.h"
  16. #include "./vp9_rtcd.h"
  17. #include "vpx_dsp/vpx_dsp_common.h"
  18. #include "vpx_dsp/postproc.h"
  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. #include "vp9/common/vp9_onyxc_int.h"
  24. #include "vp9/common/vp9_postproc.h"
  25. #if CONFIG_VP9_POSTPROC
  26. static const uint8_t q_diff_thresh = 20;
  27. static const uint8_t last_q_thresh = 170;
  28. extern const int16_t vpx_rv[];
  29. #if CONFIG_VP9_HIGHBITDEPTH
  30. static const int16_t kernel5[] = { 1, 1, 4, 1, 1 };
  31. void vp9_highbd_post_proc_down_and_across_c(const uint16_t *src_ptr,
  32. uint16_t *dst_ptr,
  33. int src_pixels_per_line,
  34. int dst_pixels_per_line, int rows,
  35. int cols, int flimit) {
  36. uint16_t const *p_src;
  37. uint16_t *p_dst;
  38. int row, col, i, v, kernel;
  39. int pitch = src_pixels_per_line;
  40. uint16_t d[8];
  41. for (row = 0; row < rows; row++) {
  42. // post_proc_down for one row.
  43. p_src = src_ptr;
  44. p_dst = dst_ptr;
  45. for (col = 0; col < cols; col++) {
  46. kernel = 4;
  47. v = p_src[col];
  48. for (i = -2; i <= 2; i++) {
  49. if (abs(v - p_src[col + i * pitch]) > flimit) goto down_skip_convolve;
  50. kernel += kernel5[2 + i] * p_src[col + i * pitch];
  51. }
  52. v = (kernel >> 3);
  53. down_skip_convolve:
  54. p_dst[col] = v;
  55. }
  56. /* now post_proc_across */
  57. p_src = dst_ptr;
  58. p_dst = dst_ptr;
  59. for (i = 0; i < 8; i++) d[i] = p_src[i];
  60. for (col = 0; col < cols; col++) {
  61. kernel = 4;
  62. v = p_src[col];
  63. d[col & 7] = v;
  64. for (i = -2; i <= 2; i++) {
  65. if (abs(v - p_src[col + i]) > flimit) goto across_skip_convolve;
  66. kernel += kernel5[2 + i] * p_src[col + i];
  67. }
  68. d[col & 7] = (kernel >> 3);
  69. across_skip_convolve:
  70. if (col >= 2) p_dst[col - 2] = d[(col - 2) & 7];
  71. }
  72. /* handle the last two pixels */
  73. p_dst[col - 2] = d[(col - 2) & 7];
  74. p_dst[col - 1] = d[(col - 1) & 7];
  75. /* next row */
  76. src_ptr += pitch;
  77. dst_ptr += dst_pixels_per_line;
  78. }
  79. }
  80. #endif // CONFIG_VP9_HIGHBITDEPTH
  81. static int q2mbl(int x) {
  82. if (x < 20) x = 20;
  83. x = 50 + (x - 50) * 10 / 8;
  84. return x * x / 3;
  85. }
  86. #if CONFIG_VP9_HIGHBITDEPTH
  87. void vp9_highbd_mbpost_proc_across_ip_c(uint16_t *src, int pitch, int rows,
  88. int cols, int flimit) {
  89. int r, c, i;
  90. uint16_t *s = src;
  91. uint16_t d[16];
  92. for (r = 0; r < rows; r++) {
  93. int sumsq = 0;
  94. int sum = 0;
  95. for (i = -8; i <= 6; i++) {
  96. sumsq += s[i] * s[i];
  97. sum += s[i];
  98. d[i + 8] = 0;
  99. }
  100. for (c = 0; c < cols + 8; c++) {
  101. int x = s[c + 7] - s[c - 8];
  102. int y = s[c + 7] + s[c - 8];
  103. sum += x;
  104. sumsq += x * y;
  105. d[c & 15] = s[c];
  106. if (sumsq * 15 - sum * sum < flimit) {
  107. d[c & 15] = (8 + sum + s[c]) >> 4;
  108. }
  109. s[c - 8] = d[(c - 8) & 15];
  110. }
  111. s += pitch;
  112. }
  113. }
  114. #endif // CONFIG_VP9_HIGHBITDEPTH
  115. #if CONFIG_VP9_HIGHBITDEPTH
  116. void vp9_highbd_mbpost_proc_down_c(uint16_t *dst, int pitch, int rows, int cols,
  117. int flimit) {
  118. int r, c, i;
  119. const int16_t *rv3 = &vpx_rv[63 & rand()]; // NOLINT
  120. for (c = 0; c < cols; c++) {
  121. uint16_t *s = &dst[c];
  122. int sumsq = 0;
  123. int sum = 0;
  124. uint16_t d[16];
  125. const int16_t *rv2 = rv3 + ((c * 17) & 127);
  126. for (i = -8; i <= 6; i++) {
  127. sumsq += s[i * pitch] * s[i * pitch];
  128. sum += s[i * pitch];
  129. }
  130. for (r = 0; r < rows + 8; r++) {
  131. sumsq += s[7 * pitch] * s[7 * pitch] - s[-8 * pitch] * s[-8 * pitch];
  132. sum += s[7 * pitch] - s[-8 * pitch];
  133. d[r & 15] = s[0];
  134. if (sumsq * 15 - sum * sum < flimit) {
  135. d[r & 15] = (rv2[r & 127] + sum + s[0]) >> 4;
  136. }
  137. s[-8 * pitch] = d[(r - 8) & 15];
  138. s += pitch;
  139. }
  140. }
  141. }
  142. #endif // CONFIG_VP9_HIGHBITDEPTH
  143. static void deblock_and_de_macro_block(YV12_BUFFER_CONFIG *source,
  144. YV12_BUFFER_CONFIG *post, int q,
  145. int low_var_thresh, int flag,
  146. uint8_t *limits) {
  147. (void)low_var_thresh;
  148. (void)flag;
  149. #if CONFIG_VP9_HIGHBITDEPTH
  150. if (source->flags & YV12_FLAG_HIGHBITDEPTH) {
  151. double level = 6.0e-05 * q * q * q - .0067 * q * q + .306 * q + .0065;
  152. int ppl = (int)(level + .5);
  153. vp9_highbd_post_proc_down_and_across(
  154. CONVERT_TO_SHORTPTR(source->y_buffer),
  155. CONVERT_TO_SHORTPTR(post->y_buffer), source->y_stride, post->y_stride,
  156. source->y_height, source->y_width, ppl);
  157. vp9_highbd_mbpost_proc_across_ip(CONVERT_TO_SHORTPTR(post->y_buffer),
  158. post->y_stride, post->y_height,
  159. post->y_width, q2mbl(q));
  160. vp9_highbd_mbpost_proc_down(CONVERT_TO_SHORTPTR(post->y_buffer),
  161. post->y_stride, post->y_height, post->y_width,
  162. q2mbl(q));
  163. vp9_highbd_post_proc_down_and_across(
  164. CONVERT_TO_SHORTPTR(source->u_buffer),
  165. CONVERT_TO_SHORTPTR(post->u_buffer), source->uv_stride, post->uv_stride,
  166. source->uv_height, source->uv_width, ppl);
  167. vp9_highbd_post_proc_down_and_across(
  168. CONVERT_TO_SHORTPTR(source->v_buffer),
  169. CONVERT_TO_SHORTPTR(post->v_buffer), source->uv_stride, post->uv_stride,
  170. source->uv_height, source->uv_width, ppl);
  171. } else {
  172. #endif // CONFIG_VP9_HIGHBITDEPTH
  173. vp9_deblock(source, post, q, limits);
  174. vpx_mbpost_proc_across_ip(post->y_buffer, post->y_stride, post->y_height,
  175. post->y_width, q2mbl(q));
  176. vpx_mbpost_proc_down(post->y_buffer, post->y_stride, post->y_height,
  177. post->y_width, q2mbl(q));
  178. #if CONFIG_VP9_HIGHBITDEPTH
  179. }
  180. #endif // CONFIG_VP9_HIGHBITDEPTH
  181. }
  182. void vp9_deblock(const YV12_BUFFER_CONFIG *src, YV12_BUFFER_CONFIG *dst, int q,
  183. uint8_t *limits) {
  184. const int ppl =
  185. (int)(6.0e-05 * q * q * q - 0.0067 * q * q + 0.306 * q + 0.0065 + 0.5);
  186. #if CONFIG_VP9_HIGHBITDEPTH
  187. if (src->flags & YV12_FLAG_HIGHBITDEPTH) {
  188. int i;
  189. const uint8_t *const srcs[3] = { src->y_buffer, src->u_buffer,
  190. src->v_buffer };
  191. const int src_strides[3] = { src->y_stride, src->uv_stride,
  192. src->uv_stride };
  193. const int src_widths[3] = { src->y_width, src->uv_width, src->uv_width };
  194. const int src_heights[3] = { src->y_height, src->uv_height,
  195. src->uv_height };
  196. uint8_t *const dsts[3] = { dst->y_buffer, dst->u_buffer, dst->v_buffer };
  197. const int dst_strides[3] = { dst->y_stride, dst->uv_stride,
  198. dst->uv_stride };
  199. for (i = 0; i < MAX_MB_PLANE; ++i) {
  200. vp9_highbd_post_proc_down_and_across(
  201. CONVERT_TO_SHORTPTR(srcs[i]), CONVERT_TO_SHORTPTR(dsts[i]),
  202. src_strides[i], dst_strides[i], src_heights[i], src_widths[i], ppl);
  203. }
  204. } else {
  205. #endif // CONFIG_VP9_HIGHBITDEPTH
  206. int mbr;
  207. const int mb_rows = src->y_height / 16;
  208. const int mb_cols = src->y_width / 16;
  209. memset(limits, (unsigned char)ppl, 16 * mb_cols);
  210. for (mbr = 0; mbr < mb_rows; mbr++) {
  211. vpx_post_proc_down_and_across_mb_row(
  212. src->y_buffer + 16 * mbr * src->y_stride,
  213. dst->y_buffer + 16 * mbr * dst->y_stride, src->y_stride,
  214. dst->y_stride, src->y_width, limits, 16);
  215. vpx_post_proc_down_and_across_mb_row(
  216. src->u_buffer + 8 * mbr * src->uv_stride,
  217. dst->u_buffer + 8 * mbr * dst->uv_stride, src->uv_stride,
  218. dst->uv_stride, src->uv_width, limits, 8);
  219. vpx_post_proc_down_and_across_mb_row(
  220. src->v_buffer + 8 * mbr * src->uv_stride,
  221. dst->v_buffer + 8 * mbr * dst->uv_stride, src->uv_stride,
  222. dst->uv_stride, src->uv_width, limits, 8);
  223. }
  224. #if CONFIG_VP9_HIGHBITDEPTH
  225. }
  226. #endif // CONFIG_VP9_HIGHBITDEPTH
  227. }
  228. void vp9_denoise(const YV12_BUFFER_CONFIG *src, YV12_BUFFER_CONFIG *dst, int q,
  229. uint8_t *limits) {
  230. vp9_deblock(src, dst, q, limits);
  231. }
  232. static void swap_mi_and_prev_mi(VP9_COMMON *cm) {
  233. // Current mip will be the prev_mip for the next frame.
  234. MODE_INFO *temp = cm->postproc_state.prev_mip;
  235. cm->postproc_state.prev_mip = cm->mip;
  236. cm->mip = temp;
  237. // Update the upper left visible macroblock ptrs.
  238. cm->mi = cm->mip + cm->mi_stride + 1;
  239. cm->postproc_state.prev_mi = cm->postproc_state.prev_mip + cm->mi_stride + 1;
  240. }
  241. int vp9_post_proc_frame(struct VP9Common *cm, YV12_BUFFER_CONFIG *dest,
  242. vp9_ppflags_t *ppflags) {
  243. const int q = VPXMIN(105, cm->lf.filter_level * 2);
  244. const int flags = ppflags->post_proc_flag;
  245. YV12_BUFFER_CONFIG *const ppbuf = &cm->post_proc_buffer;
  246. struct postproc_state *const ppstate = &cm->postproc_state;
  247. if (!cm->frame_to_show) return -1;
  248. if (!flags) {
  249. *dest = *cm->frame_to_show;
  250. return 0;
  251. }
  252. vpx_clear_system_state();
  253. // Alloc memory for prev_mip in the first frame.
  254. if (cm->current_video_frame == 1) {
  255. ppstate->last_base_qindex = cm->base_qindex;
  256. ppstate->last_frame_valid = 1;
  257. }
  258. if ((flags & VP9D_MFQE) && ppstate->prev_mip == NULL) {
  259. ppstate->prev_mip = vpx_calloc(cm->mi_alloc_size, sizeof(*cm->mip));
  260. if (!ppstate->prev_mip) {
  261. return 1;
  262. }
  263. ppstate->prev_mi = ppstate->prev_mip + cm->mi_stride + 1;
  264. }
  265. // Allocate post_proc_buffer_int if needed.
  266. if ((flags & VP9D_MFQE) && !cm->post_proc_buffer_int.buffer_alloc) {
  267. if ((flags & VP9D_DEMACROBLOCK) || (flags & VP9D_DEBLOCK)) {
  268. const int width = ALIGN_POWER_OF_TWO(cm->width, 4);
  269. const int height = ALIGN_POWER_OF_TWO(cm->height, 4);
  270. if (vpx_alloc_frame_buffer(&cm->post_proc_buffer_int, width, height,
  271. cm->subsampling_x, cm->subsampling_y,
  272. #if CONFIG_VP9_HIGHBITDEPTH
  273. cm->use_highbitdepth,
  274. #endif // CONFIG_VP9_HIGHBITDEPTH
  275. VP9_ENC_BORDER_IN_PIXELS,
  276. cm->byte_alignment) < 0) {
  277. vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
  278. "Failed to allocate MFQE framebuffer");
  279. }
  280. // Ensure that postproc is set to all 0s so that post proc
  281. // doesn't pull random data in from edge.
  282. memset(cm->post_proc_buffer_int.buffer_alloc, 128,
  283. cm->post_proc_buffer.frame_size);
  284. }
  285. }
  286. if (vpx_realloc_frame_buffer(&cm->post_proc_buffer, cm->width, cm->height,
  287. cm->subsampling_x, cm->subsampling_y,
  288. #if CONFIG_VP9_HIGHBITDEPTH
  289. cm->use_highbitdepth,
  290. #endif
  291. VP9_DEC_BORDER_IN_PIXELS, cm->byte_alignment,
  292. NULL, NULL, NULL) < 0)
  293. vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
  294. "Failed to allocate post-processing buffer");
  295. if (flags & (VP9D_DEMACROBLOCK | VP9D_DEBLOCK)) {
  296. if (!cm->postproc_state.limits) {
  297. cm->postproc_state.limits =
  298. vpx_calloc(cm->width, sizeof(*cm->postproc_state.limits));
  299. }
  300. }
  301. if (flags & VP9D_ADDNOISE) {
  302. if (!cm->postproc_state.generated_noise) {
  303. cm->postproc_state.generated_noise = vpx_calloc(
  304. cm->width + 256, sizeof(*cm->postproc_state.generated_noise));
  305. if (!cm->postproc_state.generated_noise) return 1;
  306. }
  307. }
  308. if ((flags & VP9D_MFQE) && cm->current_video_frame >= 2 &&
  309. ppstate->last_frame_valid && cm->bit_depth == 8 &&
  310. ppstate->last_base_qindex <= last_q_thresh &&
  311. cm->base_qindex - ppstate->last_base_qindex >= q_diff_thresh) {
  312. vp9_mfqe(cm);
  313. // TODO(jackychen): Consider whether enable deblocking by default
  314. // if mfqe is enabled. Need to take both the quality and the speed
  315. // into consideration.
  316. if ((flags & VP9D_DEMACROBLOCK) || (flags & VP9D_DEBLOCK)) {
  317. vpx_yv12_copy_frame(ppbuf, &cm->post_proc_buffer_int);
  318. }
  319. if ((flags & VP9D_DEMACROBLOCK) && cm->post_proc_buffer_int.buffer_alloc) {
  320. deblock_and_de_macro_block(&cm->post_proc_buffer_int, ppbuf,
  321. q + (ppflags->deblocking_level - 5) * 10, 1, 0,
  322. cm->postproc_state.limits);
  323. } else if (flags & VP9D_DEBLOCK) {
  324. vp9_deblock(&cm->post_proc_buffer_int, ppbuf, q,
  325. cm->postproc_state.limits);
  326. } else {
  327. vpx_yv12_copy_frame(&cm->post_proc_buffer_int, ppbuf);
  328. }
  329. } else if (flags & VP9D_DEMACROBLOCK) {
  330. deblock_and_de_macro_block(cm->frame_to_show, ppbuf,
  331. q + (ppflags->deblocking_level - 5) * 10, 1, 0,
  332. cm->postproc_state.limits);
  333. } else if (flags & VP9D_DEBLOCK) {
  334. vp9_deblock(cm->frame_to_show, ppbuf, q, cm->postproc_state.limits);
  335. } else {
  336. vpx_yv12_copy_frame(cm->frame_to_show, ppbuf);
  337. }
  338. ppstate->last_base_qindex = cm->base_qindex;
  339. ppstate->last_frame_valid = 1;
  340. if (flags & VP9D_ADDNOISE) {
  341. const int noise_level = ppflags->noise_level;
  342. if (ppstate->last_q != q || ppstate->last_noise != noise_level) {
  343. double sigma;
  344. vpx_clear_system_state();
  345. sigma = noise_level + .5 + .6 * q / 63.0;
  346. ppstate->clamp =
  347. vpx_setup_noise(sigma, ppstate->generated_noise, cm->width + 256);
  348. ppstate->last_q = q;
  349. ppstate->last_noise = noise_level;
  350. }
  351. vpx_plane_add_noise(ppbuf->y_buffer, ppstate->generated_noise,
  352. ppstate->clamp, ppstate->clamp, ppbuf->y_width,
  353. ppbuf->y_height, ppbuf->y_stride);
  354. }
  355. *dest = *ppbuf;
  356. /* handle problem with extending borders */
  357. dest->y_width = cm->width;
  358. dest->y_height = cm->height;
  359. dest->uv_width = dest->y_width >> cm->subsampling_x;
  360. dest->uv_height = dest->y_height >> cm->subsampling_y;
  361. if (flags & VP9D_MFQE) swap_mi_and_prev_mi(cm);
  362. return 0;
  363. }
  364. #endif // CONFIG_VP9_POSTPROC