postproc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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->show_frame_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. /* Blend the macro block with a solid colored square. Leave the
  136. * edges unblended to give distinction to macro blocks in areas
  137. * filled with the same color block.
  138. */
  139. void vp8_blend_mb_inner_c(unsigned char *y, unsigned char *u, unsigned char *v,
  140. int y_1, int u_1, int v_1, int alpha, int stride) {
  141. int i, j;
  142. int y1_const = y_1 * ((1 << 16) - alpha);
  143. int u1_const = u_1 * ((1 << 16) - alpha);
  144. int v1_const = v_1 * ((1 << 16) - alpha);
  145. y += 2 * stride + 2;
  146. for (i = 0; i < 12; ++i) {
  147. for (j = 0; j < 12; ++j) {
  148. y[j] = (y[j] * alpha + y1_const) >> 16;
  149. }
  150. y += stride;
  151. }
  152. stride >>= 1;
  153. u += stride + 1;
  154. v += stride + 1;
  155. for (i = 0; i < 6; ++i) {
  156. for (j = 0; j < 6; ++j) {
  157. u[j] = (u[j] * alpha + u1_const) >> 16;
  158. v[j] = (v[j] * alpha + v1_const) >> 16;
  159. }
  160. u += stride;
  161. v += stride;
  162. }
  163. }
  164. /* Blend only the edge of the macro block. Leave center
  165. * unblended to allow for other visualizations to be layered.
  166. */
  167. void vp8_blend_mb_outer_c(unsigned char *y, unsigned char *u, unsigned char *v,
  168. int y_1, int u_1, int v_1, int alpha, int stride) {
  169. int i, j;
  170. int y1_const = y_1 * ((1 << 16) - alpha);
  171. int u1_const = u_1 * ((1 << 16) - alpha);
  172. int v1_const = v_1 * ((1 << 16) - alpha);
  173. for (i = 0; i < 2; ++i) {
  174. for (j = 0; j < 16; ++j) {
  175. y[j] = (y[j] * alpha + y1_const) >> 16;
  176. }
  177. y += stride;
  178. }
  179. for (i = 0; i < 12; ++i) {
  180. y[0] = (y[0] * alpha + y1_const) >> 16;
  181. y[1] = (y[1] * alpha + y1_const) >> 16;
  182. y[14] = (y[14] * alpha + y1_const) >> 16;
  183. y[15] = (y[15] * alpha + y1_const) >> 16;
  184. y += stride;
  185. }
  186. for (i = 0; i < 2; ++i) {
  187. for (j = 0; j < 16; ++j) {
  188. y[j] = (y[j] * alpha + y1_const) >> 16;
  189. }
  190. y += stride;
  191. }
  192. stride >>= 1;
  193. for (j = 0; j < 8; ++j) {
  194. u[j] = (u[j] * alpha + u1_const) >> 16;
  195. v[j] = (v[j] * alpha + v1_const) >> 16;
  196. }
  197. u += stride;
  198. v += stride;
  199. for (i = 0; i < 6; ++i) {
  200. u[0] = (u[0] * alpha + u1_const) >> 16;
  201. v[0] = (v[0] * alpha + v1_const) >> 16;
  202. u[7] = (u[7] * alpha + u1_const) >> 16;
  203. v[7] = (v[7] * alpha + v1_const) >> 16;
  204. u += stride;
  205. v += stride;
  206. }
  207. for (j = 0; j < 8; ++j) {
  208. u[j] = (u[j] * alpha + u1_const) >> 16;
  209. v[j] = (v[j] * alpha + v1_const) >> 16;
  210. }
  211. }
  212. void vp8_blend_b_c(unsigned char *y, unsigned char *u, unsigned char *v,
  213. int y_1, int u_1, int v_1, int alpha, int stride) {
  214. int i, j;
  215. int y1_const = y_1 * ((1 << 16) - alpha);
  216. int u1_const = u_1 * ((1 << 16) - alpha);
  217. int v1_const = v_1 * ((1 << 16) - alpha);
  218. for (i = 0; i < 4; ++i) {
  219. for (j = 0; j < 4; ++j) {
  220. y[j] = (y[j] * alpha + y1_const) >> 16;
  221. }
  222. y += stride;
  223. }
  224. stride >>= 1;
  225. for (i = 0; i < 2; ++i) {
  226. for (j = 0; j < 2; ++j) {
  227. u[j] = (u[j] * alpha + u1_const) >> 16;
  228. v[j] = (v[j] * alpha + v1_const) >> 16;
  229. }
  230. u += stride;
  231. v += stride;
  232. }
  233. }
  234. #if CONFIG_POSTPROC
  235. int vp8_post_proc_frame(VP8_COMMON *oci, YV12_BUFFER_CONFIG *dest,
  236. vp8_ppflags_t *ppflags) {
  237. int q = oci->filter_level * 10 / 6;
  238. int flags = ppflags->post_proc_flag;
  239. int deblock_level = ppflags->deblocking_level;
  240. int noise_level = ppflags->noise_level;
  241. if (!oci->frame_to_show) return -1;
  242. if (q > 63) q = 63;
  243. if (!flags) {
  244. *dest = *oci->frame_to_show;
  245. /* handle problem with extending borders */
  246. dest->y_width = oci->Width;
  247. dest->y_height = oci->Height;
  248. dest->uv_height = dest->y_height / 2;
  249. oci->postproc_state.last_base_qindex = oci->base_qindex;
  250. oci->postproc_state.last_frame_valid = 1;
  251. return 0;
  252. }
  253. if (flags & VP8D_ADDNOISE) {
  254. if (!oci->postproc_state.generated_noise) {
  255. oci->postproc_state.generated_noise = vpx_calloc(
  256. oci->Width + 256, sizeof(*oci->postproc_state.generated_noise));
  257. if (!oci->postproc_state.generated_noise) return 1;
  258. }
  259. }
  260. /* Allocate post_proc_buffer_int if needed */
  261. if ((flags & VP8D_MFQE) && !oci->post_proc_buffer_int_used) {
  262. if ((flags & VP8D_DEBLOCK) || (flags & VP8D_DEMACROBLOCK)) {
  263. int width = (oci->Width + 15) & ~15;
  264. int height = (oci->Height + 15) & ~15;
  265. if (vp8_yv12_alloc_frame_buffer(&oci->post_proc_buffer_int, width, height,
  266. VP8BORDERINPIXELS)) {
  267. vpx_internal_error(&oci->error, VPX_CODEC_MEM_ERROR,
  268. "Failed to allocate MFQE framebuffer");
  269. }
  270. oci->post_proc_buffer_int_used = 1;
  271. /* insure that postproc is set to all 0's so that post proc
  272. * doesn't pull random data in from edge
  273. */
  274. memset((&oci->post_proc_buffer_int)->buffer_alloc, 128,
  275. (&oci->post_proc_buffer)->frame_size);
  276. }
  277. }
  278. vpx_clear_system_state();
  279. if ((flags & VP8D_MFQE) && oci->postproc_state.last_frame_valid &&
  280. oci->current_video_frame >= 2 &&
  281. oci->postproc_state.last_base_qindex < 60 &&
  282. oci->base_qindex - oci->postproc_state.last_base_qindex >= 20) {
  283. vp8_multiframe_quality_enhance(oci);
  284. if (((flags & VP8D_DEBLOCK) || (flags & VP8D_DEMACROBLOCK)) &&
  285. oci->post_proc_buffer_int_used) {
  286. vp8_yv12_copy_frame(&oci->post_proc_buffer, &oci->post_proc_buffer_int);
  287. if (flags & VP8D_DEMACROBLOCK) {
  288. vp8_deblock(oci, &oci->post_proc_buffer_int, &oci->post_proc_buffer,
  289. q + (deblock_level - 5) * 10, 1, 0);
  290. vp8_de_mblock(&oci->post_proc_buffer, q + (deblock_level - 5) * 10);
  291. } else if (flags & VP8D_DEBLOCK) {
  292. vp8_deblock(oci, &oci->post_proc_buffer_int, &oci->post_proc_buffer, q,
  293. 1, 0);
  294. }
  295. }
  296. /* Move partially towards the base q of the previous frame */
  297. oci->postproc_state.last_base_qindex =
  298. (3 * oci->postproc_state.last_base_qindex + oci->base_qindex) >> 2;
  299. } else if (flags & VP8D_DEMACROBLOCK) {
  300. vp8_deblock(oci, oci->frame_to_show, &oci->post_proc_buffer,
  301. q + (deblock_level - 5) * 10, 1, 0);
  302. vp8_de_mblock(&oci->post_proc_buffer, q + (deblock_level - 5) * 10);
  303. oci->postproc_state.last_base_qindex = oci->base_qindex;
  304. } else if (flags & VP8D_DEBLOCK) {
  305. vp8_deblock(oci, oci->frame_to_show, &oci->post_proc_buffer, q, 1, 0);
  306. oci->postproc_state.last_base_qindex = oci->base_qindex;
  307. } else {
  308. vp8_yv12_copy_frame(oci->frame_to_show, &oci->post_proc_buffer);
  309. oci->postproc_state.last_base_qindex = oci->base_qindex;
  310. }
  311. oci->postproc_state.last_frame_valid = 1;
  312. if (flags & VP8D_ADDNOISE) {
  313. if (oci->postproc_state.last_q != q ||
  314. oci->postproc_state.last_noise != noise_level) {
  315. double sigma;
  316. struct postproc_state *ppstate = &oci->postproc_state;
  317. vpx_clear_system_state();
  318. sigma = noise_level + .5 + .6 * q / 63.0;
  319. ppstate->clamp =
  320. vpx_setup_noise(sigma, ppstate->generated_noise, oci->Width + 256);
  321. ppstate->last_q = q;
  322. ppstate->last_noise = noise_level;
  323. }
  324. vpx_plane_add_noise(
  325. oci->post_proc_buffer.y_buffer, oci->postproc_state.generated_noise,
  326. oci->postproc_state.clamp, oci->postproc_state.clamp,
  327. oci->post_proc_buffer.y_width, oci->post_proc_buffer.y_height,
  328. oci->post_proc_buffer.y_stride);
  329. }
  330. *dest = oci->post_proc_buffer;
  331. /* handle problem with extending borders */
  332. dest->y_width = oci->Width;
  333. dest->y_height = oci->Height;
  334. dest->uv_height = dest->y_height / 2;
  335. return 0;
  336. }
  337. #endif