2
0

postproc.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 "vpx_config.h"
  11. #include "vpx_dsp_rtcd.h"
  12. #include "vp8_rtcd.h"
  13. #include "vpx_dsp/postproc.h"
  14. #include "vpx_ports/system_state.h"
  15. #include "vpx_scale_rtcd.h"
  16. #include "vpx_scale/yv12config.h"
  17. #include "postproc.h"
  18. #include "common.h"
  19. #include "vpx_scale/vpx_scale.h"
  20. #include "systemdependent.h"
  21. #include <limits.h>
  22. #include <math.h>
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. /* clang-format off */
  26. #define RGB_TO_YUV(t) \
  27. (unsigned char)((0.257 * (float)(t >> 16)) + \
  28. (0.504 * (float)(t >> 8 & 0xff)) + \
  29. (0.098 * (float)(t & 0xff)) + 16), \
  30. (unsigned char)(-(0.148 * (float)(t >> 16)) - \
  31. (0.291 * (float)(t >> 8 & 0xff)) + \
  32. (0.439 * (float)(t & 0xff)) + 128), \
  33. (unsigned char)((0.439 * (float)(t >> 16)) - \
  34. (0.368 * (float)(t >> 8 & 0xff)) - \
  35. (0.071 * (float)(t & 0xff)) + 128)
  36. /* clang-format on */
  37. extern void vp8_blit_text(const char *msg, unsigned char *address,
  38. const int pitch);
  39. extern void vp8_blit_line(int x0, int x1, int y0, int y1, unsigned char *image,
  40. const int pitch);
  41. /***********************************************************************************************************
  42. */
  43. #if CONFIG_POSTPROC
  44. static int q2mbl(int x) {
  45. if (x < 20) x = 20;
  46. x = 50 + (x - 50) * 10 / 8;
  47. return x * x / 3;
  48. }
  49. static void vp8_de_mblock(YV12_BUFFER_CONFIG *post, int q) {
  50. vpx_mbpost_proc_across_ip(post->y_buffer, post->y_stride, post->y_height,
  51. post->y_width, q2mbl(q));
  52. vpx_mbpost_proc_down(post->y_buffer, post->y_stride, post->y_height,
  53. post->y_width, q2mbl(q));
  54. }
  55. void vp8_deblock(VP8_COMMON *cm, YV12_BUFFER_CONFIG *source,
  56. YV12_BUFFER_CONFIG *post, int q, int low_var_thresh,
  57. int flag) {
  58. double level = 6.0e-05 * q * q * q - .0067 * q * q + .306 * q + .0065;
  59. int ppl = (int)(level + .5);
  60. const MODE_INFO *mode_info_context = cm->mi;
  61. int mbr, mbc;
  62. /* The pixel thresholds are adjusted according to if or not the macroblock
  63. * is a skipped block. */
  64. unsigned char *ylimits = cm->pp_limits_buffer;
  65. unsigned char *uvlimits = cm->pp_limits_buffer + 16 * cm->mb_cols;
  66. (void)low_var_thresh;
  67. (void)flag;
  68. if (ppl > 0) {
  69. for (mbr = 0; mbr < cm->mb_rows; ++mbr) {
  70. unsigned char *ylptr = ylimits;
  71. unsigned char *uvlptr = uvlimits;
  72. for (mbc = 0; mbc < cm->mb_cols; ++mbc) {
  73. unsigned char mb_ppl;
  74. if (mode_info_context->mbmi.mb_skip_coeff) {
  75. mb_ppl = (unsigned char)ppl >> 1;
  76. } else {
  77. mb_ppl = (unsigned char)ppl;
  78. }
  79. memset(ylptr, mb_ppl, 16);
  80. memset(uvlptr, mb_ppl, 8);
  81. ylptr += 16;
  82. uvlptr += 8;
  83. mode_info_context++;
  84. }
  85. mode_info_context++;
  86. vpx_post_proc_down_and_across_mb_row(
  87. source->y_buffer + 16 * mbr * source->y_stride,
  88. post->y_buffer + 16 * mbr * post->y_stride, source->y_stride,
  89. post->y_stride, source->y_width, ylimits, 16);
  90. vpx_post_proc_down_and_across_mb_row(
  91. source->u_buffer + 8 * mbr * source->uv_stride,
  92. post->u_buffer + 8 * mbr * post->uv_stride, source->uv_stride,
  93. post->uv_stride, source->uv_width, uvlimits, 8);
  94. vpx_post_proc_down_and_across_mb_row(
  95. source->v_buffer + 8 * mbr * source->uv_stride,
  96. post->v_buffer + 8 * mbr * post->uv_stride, source->uv_stride,
  97. post->uv_stride, source->uv_width, uvlimits, 8);
  98. }
  99. } else {
  100. vp8_yv12_copy_frame(source, post);
  101. }
  102. }
  103. void vp8_de_noise(VP8_COMMON *cm, YV12_BUFFER_CONFIG *source,
  104. YV12_BUFFER_CONFIG *post, int q, int low_var_thresh, int flag,
  105. int uvfilter) {
  106. int mbr;
  107. double level = 6.0e-05 * q * q * q - .0067 * q * q + .306 * q + .0065;
  108. int ppl = (int)(level + .5);
  109. int mb_rows = cm->mb_rows;
  110. int mb_cols = cm->mb_cols;
  111. unsigned char *limits = cm->pp_limits_buffer;
  112. (void)post;
  113. (void)low_var_thresh;
  114. (void)flag;
  115. memset(limits, (unsigned char)ppl, 16 * mb_cols);
  116. /* TODO: The original code don't filter the 2 outer rows and columns. */
  117. for (mbr = 0; mbr < mb_rows; ++mbr) {
  118. vpx_post_proc_down_and_across_mb_row(
  119. source->y_buffer + 16 * mbr * source->y_stride,
  120. source->y_buffer + 16 * mbr * source->y_stride, source->y_stride,
  121. source->y_stride, source->y_width, limits, 16);
  122. if (uvfilter == 1) {
  123. vpx_post_proc_down_and_across_mb_row(
  124. source->u_buffer + 8 * mbr * source->uv_stride,
  125. source->u_buffer + 8 * mbr * source->uv_stride, source->uv_stride,
  126. source->uv_stride, source->uv_width, limits, 8);
  127. vpx_post_proc_down_and_across_mb_row(
  128. source->v_buffer + 8 * mbr * source->uv_stride,
  129. source->v_buffer + 8 * mbr * source->uv_stride, source->uv_stride,
  130. source->uv_stride, source->uv_width, limits, 8);
  131. }
  132. }
  133. }
  134. #endif // CONFIG_POSTPROC
  135. #if CONFIG_POSTPROC
  136. int vp8_post_proc_frame(VP8_COMMON *oci, YV12_BUFFER_CONFIG *dest,
  137. vp8_ppflags_t *ppflags) {
  138. int q = oci->filter_level * 10 / 6;
  139. int flags = ppflags->post_proc_flag;
  140. int deblock_level = ppflags->deblocking_level;
  141. int noise_level = ppflags->noise_level;
  142. if (!oci->frame_to_show) return -1;
  143. if (q > 63) q = 63;
  144. if (!flags) {
  145. *dest = *oci->frame_to_show;
  146. /* handle problem with extending borders */
  147. dest->y_width = oci->Width;
  148. dest->y_height = oci->Height;
  149. dest->uv_height = dest->y_height / 2;
  150. oci->postproc_state.last_base_qindex = oci->base_qindex;
  151. oci->postproc_state.last_frame_valid = 1;
  152. return 0;
  153. }
  154. if (flags & VP8D_ADDNOISE) {
  155. if (!oci->postproc_state.generated_noise) {
  156. oci->postproc_state.generated_noise = vpx_calloc(
  157. oci->Width + 256, sizeof(*oci->postproc_state.generated_noise));
  158. if (!oci->postproc_state.generated_noise) return 1;
  159. }
  160. }
  161. /* Allocate post_proc_buffer_int if needed */
  162. if ((flags & VP8D_MFQE) && !oci->post_proc_buffer_int_used) {
  163. if ((flags & VP8D_DEBLOCK) || (flags & VP8D_DEMACROBLOCK)) {
  164. int width = (oci->Width + 15) & ~15;
  165. int height = (oci->Height + 15) & ~15;
  166. if (vp8_yv12_alloc_frame_buffer(&oci->post_proc_buffer_int, width, height,
  167. VP8BORDERINPIXELS)) {
  168. vpx_internal_error(&oci->error, VPX_CODEC_MEM_ERROR,
  169. "Failed to allocate MFQE framebuffer");
  170. }
  171. oci->post_proc_buffer_int_used = 1;
  172. /* insure that postproc is set to all 0's so that post proc
  173. * doesn't pull random data in from edge
  174. */
  175. memset((&oci->post_proc_buffer_int)->buffer_alloc, 128,
  176. (&oci->post_proc_buffer)->frame_size);
  177. }
  178. }
  179. vpx_clear_system_state();
  180. if ((flags & VP8D_MFQE) && oci->postproc_state.last_frame_valid &&
  181. oci->current_video_frame > 10 &&
  182. oci->postproc_state.last_base_qindex < 60 &&
  183. oci->base_qindex - oci->postproc_state.last_base_qindex >= 20) {
  184. vp8_multiframe_quality_enhance(oci);
  185. if (((flags & VP8D_DEBLOCK) || (flags & VP8D_DEMACROBLOCK)) &&
  186. oci->post_proc_buffer_int_used) {
  187. vp8_yv12_copy_frame(&oci->post_proc_buffer, &oci->post_proc_buffer_int);
  188. if (flags & VP8D_DEMACROBLOCK) {
  189. vp8_deblock(oci, &oci->post_proc_buffer_int, &oci->post_proc_buffer,
  190. q + (deblock_level - 5) * 10, 1, 0);
  191. vp8_de_mblock(&oci->post_proc_buffer, q + (deblock_level - 5) * 10);
  192. } else if (flags & VP8D_DEBLOCK) {
  193. vp8_deblock(oci, &oci->post_proc_buffer_int, &oci->post_proc_buffer, q,
  194. 1, 0);
  195. }
  196. }
  197. /* Move partially towards the base q of the previous frame */
  198. oci->postproc_state.last_base_qindex =
  199. (3 * oci->postproc_state.last_base_qindex + oci->base_qindex) >> 2;
  200. } else if (flags & VP8D_DEMACROBLOCK) {
  201. vp8_deblock(oci, oci->frame_to_show, &oci->post_proc_buffer,
  202. q + (deblock_level - 5) * 10, 1, 0);
  203. vp8_de_mblock(&oci->post_proc_buffer, q + (deblock_level - 5) * 10);
  204. oci->postproc_state.last_base_qindex = oci->base_qindex;
  205. } else if (flags & VP8D_DEBLOCK) {
  206. vp8_deblock(oci, oci->frame_to_show, &oci->post_proc_buffer, q, 1, 0);
  207. oci->postproc_state.last_base_qindex = oci->base_qindex;
  208. } else {
  209. vp8_yv12_copy_frame(oci->frame_to_show, &oci->post_proc_buffer);
  210. oci->postproc_state.last_base_qindex = oci->base_qindex;
  211. }
  212. oci->postproc_state.last_frame_valid = 1;
  213. if (flags & VP8D_ADDNOISE) {
  214. if (oci->postproc_state.last_q != q ||
  215. oci->postproc_state.last_noise != noise_level) {
  216. double sigma;
  217. struct postproc_state *ppstate = &oci->postproc_state;
  218. vpx_clear_system_state();
  219. sigma = noise_level + .5 + .6 * q / 63.0;
  220. ppstate->clamp =
  221. vpx_setup_noise(sigma, ppstate->generated_noise, oci->Width + 256);
  222. ppstate->last_q = q;
  223. ppstate->last_noise = noise_level;
  224. }
  225. vpx_plane_add_noise(
  226. oci->post_proc_buffer.y_buffer, oci->postproc_state.generated_noise,
  227. oci->postproc_state.clamp, oci->postproc_state.clamp,
  228. oci->post_proc_buffer.y_width, oci->post_proc_buffer.y_height,
  229. oci->post_proc_buffer.y_stride);
  230. }
  231. *dest = oci->post_proc_buffer;
  232. /* handle problem with extending borders */
  233. dest->y_width = oci->Width;
  234. dest->y_height = oci->Height;
  235. dest->uv_height = dest->y_height / 2;
  236. return 0;
  237. }
  238. #endif