postproc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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_scale_rtcd.h"
  15. #include "vpx_scale/yv12config.h"
  16. #include "postproc.h"
  17. #include "common.h"
  18. #include "vpx_scale/vpx_scale.h"
  19. #include "systemdependent.h"
  20. #include <limits.h>
  21. #include <math.h>
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. /* clang-format off */
  25. #define RGB_TO_YUV(t) \
  26. (unsigned char)((0.257 * (float)(t >> 16)) + \
  27. (0.504 * (float)(t >> 8 & 0xff)) + \
  28. (0.098 * (float)(t & 0xff)) + 16), \
  29. (unsigned char)(-(0.148 * (float)(t >> 16)) - \
  30. (0.291 * (float)(t >> 8 & 0xff)) + \
  31. (0.439 * (float)(t & 0xff)) + 128), \
  32. (unsigned char)((0.439 * (float)(t >> 16)) - \
  33. (0.368 * (float)(t >> 8 & 0xff)) - \
  34. (0.071 * (float)(t & 0xff)) + 128)
  35. /* clang-format on */
  36. extern void vp8_blit_text(const char *msg, unsigned char *address,
  37. const int pitch);
  38. extern void vp8_blit_line(int x0, int x1, int y0, int y1, unsigned char *image,
  39. const int pitch);
  40. /***********************************************************************************************************
  41. */
  42. #if CONFIG_POSTPROC
  43. static int q2mbl(int x) {
  44. if (x < 20) x = 20;
  45. x = 50 + (x - 50) * 10 / 8;
  46. return x * x / 3;
  47. }
  48. static void vp8_de_mblock(YV12_BUFFER_CONFIG *post, int q) {
  49. vpx_mbpost_proc_across_ip(post->y_buffer, post->y_stride, post->y_height,
  50. post->y_width, q2mbl(q));
  51. vpx_mbpost_proc_down(post->y_buffer, post->y_stride, post->y_height,
  52. post->y_width, q2mbl(q));
  53. }
  54. void vp8_deblock(VP8_COMMON *cm, YV12_BUFFER_CONFIG *source,
  55. YV12_BUFFER_CONFIG *post, int q, int low_var_thresh,
  56. int flag) {
  57. double level = 6.0e-05 * q * q * q - .0067 * q * q + .306 * q + .0065;
  58. int ppl = (int)(level + .5);
  59. const MODE_INFO *mode_info_context = cm->show_frame_mi;
  60. int mbr, mbc;
  61. /* The pixel thresholds are adjusted according to if or not the macroblock
  62. * is a skipped block. */
  63. unsigned char *ylimits = cm->pp_limits_buffer;
  64. unsigned char *uvlimits = cm->pp_limits_buffer + 16 * cm->mb_cols;
  65. (void)low_var_thresh;
  66. (void)flag;
  67. if (ppl > 0) {
  68. for (mbr = 0; mbr < cm->mb_rows; ++mbr) {
  69. unsigned char *ylptr = ylimits;
  70. unsigned char *uvlptr = uvlimits;
  71. for (mbc = 0; mbc < cm->mb_cols; ++mbc) {
  72. unsigned char mb_ppl;
  73. if (mode_info_context->mbmi.mb_skip_coeff) {
  74. mb_ppl = (unsigned char)ppl >> 1;
  75. } else {
  76. mb_ppl = (unsigned char)ppl;
  77. }
  78. memset(ylptr, mb_ppl, 16);
  79. memset(uvlptr, mb_ppl, 8);
  80. ylptr += 16;
  81. uvlptr += 8;
  82. mode_info_context++;
  83. }
  84. mode_info_context++;
  85. vpx_post_proc_down_and_across_mb_row(
  86. source->y_buffer + 16 * mbr * source->y_stride,
  87. post->y_buffer + 16 * mbr * post->y_stride, source->y_stride,
  88. post->y_stride, source->y_width, ylimits, 16);
  89. vpx_post_proc_down_and_across_mb_row(
  90. source->u_buffer + 8 * mbr * source->uv_stride,
  91. post->u_buffer + 8 * mbr * post->uv_stride, source->uv_stride,
  92. post->uv_stride, source->uv_width, uvlimits, 8);
  93. vpx_post_proc_down_and_across_mb_row(
  94. source->v_buffer + 8 * mbr * source->uv_stride,
  95. post->v_buffer + 8 * mbr * post->uv_stride, source->uv_stride,
  96. post->uv_stride, source->uv_width, uvlimits, 8);
  97. }
  98. } else {
  99. vp8_yv12_copy_frame(source, post);
  100. }
  101. }
  102. void vp8_de_noise(VP8_COMMON *cm, YV12_BUFFER_CONFIG *source,
  103. YV12_BUFFER_CONFIG *post, int q, int low_var_thresh, int flag,
  104. int uvfilter) {
  105. int mbr;
  106. double level = 6.0e-05 * q * q * q - .0067 * q * q + .306 * q + .0065;
  107. int ppl = (int)(level + .5);
  108. int mb_rows = cm->mb_rows;
  109. int mb_cols = cm->mb_cols;
  110. unsigned char *limits = cm->pp_limits_buffer;
  111. (void)post;
  112. (void)low_var_thresh;
  113. (void)flag;
  114. memset(limits, (unsigned char)ppl, 16 * mb_cols);
  115. /* TODO: The original code don't filter the 2 outer rows and columns. */
  116. for (mbr = 0; mbr < mb_rows; ++mbr) {
  117. vpx_post_proc_down_and_across_mb_row(
  118. source->y_buffer + 16 * mbr * source->y_stride,
  119. source->y_buffer + 16 * mbr * source->y_stride, source->y_stride,
  120. source->y_stride, source->y_width, limits, 16);
  121. if (uvfilter == 1) {
  122. vpx_post_proc_down_and_across_mb_row(
  123. source->u_buffer + 8 * mbr * source->uv_stride,
  124. source->u_buffer + 8 * mbr * source->uv_stride, source->uv_stride,
  125. source->uv_stride, source->uv_width, limits, 8);
  126. vpx_post_proc_down_and_across_mb_row(
  127. source->v_buffer + 8 * mbr * source->uv_stride,
  128. source->v_buffer + 8 * mbr * source->uv_stride, source->uv_stride,
  129. source->uv_stride, source->uv_width, limits, 8);
  130. }
  131. }
  132. }
  133. #endif // CONFIG_POSTPROC
  134. /* Blend the macro block with a solid colored square. Leave the
  135. * edges unblended to give distinction to macro blocks in areas
  136. * filled with the same color block.
  137. */
  138. void vp8_blend_mb_inner_c(unsigned char *y, unsigned char *u, unsigned char *v,
  139. int y_1, int u_1, int v_1, int alpha, int stride) {
  140. int i, j;
  141. int y1_const = y_1 * ((1 << 16) - alpha);
  142. int u1_const = u_1 * ((1 << 16) - alpha);
  143. int v1_const = v_1 * ((1 << 16) - alpha);
  144. y += 2 * stride + 2;
  145. for (i = 0; i < 12; ++i) {
  146. for (j = 0; j < 12; ++j) {
  147. y[j] = (y[j] * alpha + y1_const) >> 16;
  148. }
  149. y += stride;
  150. }
  151. stride >>= 1;
  152. u += stride + 1;
  153. v += stride + 1;
  154. for (i = 0; i < 6; ++i) {
  155. for (j = 0; j < 6; ++j) {
  156. u[j] = (u[j] * alpha + u1_const) >> 16;
  157. v[j] = (v[j] * alpha + v1_const) >> 16;
  158. }
  159. u += stride;
  160. v += stride;
  161. }
  162. }
  163. /* Blend only the edge of the macro block. Leave center
  164. * unblended to allow for other visualizations to be layered.
  165. */
  166. void vp8_blend_mb_outer_c(unsigned char *y, unsigned char *u, unsigned char *v,
  167. int y_1, int u_1, int v_1, int alpha, int stride) {
  168. int i, j;
  169. int y1_const = y_1 * ((1 << 16) - alpha);
  170. int u1_const = u_1 * ((1 << 16) - alpha);
  171. int v1_const = v_1 * ((1 << 16) - alpha);
  172. for (i = 0; i < 2; ++i) {
  173. for (j = 0; j < 16; ++j) {
  174. y[j] = (y[j] * alpha + y1_const) >> 16;
  175. }
  176. y += stride;
  177. }
  178. for (i = 0; i < 12; ++i) {
  179. y[0] = (y[0] * alpha + y1_const) >> 16;
  180. y[1] = (y[1] * alpha + y1_const) >> 16;
  181. y[14] = (y[14] * alpha + y1_const) >> 16;
  182. y[15] = (y[15] * alpha + y1_const) >> 16;
  183. y += stride;
  184. }
  185. for (i = 0; i < 2; ++i) {
  186. for (j = 0; j < 16; ++j) {
  187. y[j] = (y[j] * alpha + y1_const) >> 16;
  188. }
  189. y += stride;
  190. }
  191. stride >>= 1;
  192. for (j = 0; j < 8; ++j) {
  193. u[j] = (u[j] * alpha + u1_const) >> 16;
  194. v[j] = (v[j] * alpha + v1_const) >> 16;
  195. }
  196. u += stride;
  197. v += stride;
  198. for (i = 0; i < 6; ++i) {
  199. u[0] = (u[0] * alpha + u1_const) >> 16;
  200. v[0] = (v[0] * alpha + v1_const) >> 16;
  201. u[7] = (u[7] * alpha + u1_const) >> 16;
  202. v[7] = (v[7] * alpha + v1_const) >> 16;
  203. u += stride;
  204. v += stride;
  205. }
  206. for (j = 0; j < 8; ++j) {
  207. u[j] = (u[j] * alpha + u1_const) >> 16;
  208. v[j] = (v[j] * alpha + v1_const) >> 16;
  209. }
  210. }
  211. void vp8_blend_b_c(unsigned char *y, unsigned char *u, unsigned char *v,
  212. int y_1, int u_1, int v_1, int alpha, int stride) {
  213. int i, j;
  214. int y1_const = y_1 * ((1 << 16) - alpha);
  215. int u1_const = u_1 * ((1 << 16) - alpha);
  216. int v1_const = v_1 * ((1 << 16) - alpha);
  217. for (i = 0; i < 4; ++i) {
  218. for (j = 0; j < 4; ++j) {
  219. y[j] = (y[j] * alpha + y1_const) >> 16;
  220. }
  221. y += stride;
  222. }
  223. stride >>= 1;
  224. for (i = 0; i < 2; ++i) {
  225. for (j = 0; j < 2; ++j) {
  226. u[j] = (u[j] * alpha + u1_const) >> 16;
  227. v[j] = (v[j] * alpha + v1_const) >> 16;
  228. }
  229. u += stride;
  230. v += stride;
  231. }
  232. }
  233. #if CONFIG_POSTPROC
  234. int vp8_post_proc_frame(VP8_COMMON *oci, YV12_BUFFER_CONFIG *dest,
  235. vp8_ppflags_t *ppflags) {
  236. int q = oci->filter_level * 10 / 6;
  237. int flags = ppflags->post_proc_flag;
  238. int deblock_level = ppflags->deblocking_level;
  239. int noise_level = ppflags->noise_level;
  240. if (!oci->frame_to_show) return -1;
  241. if (q > 63) q = 63;
  242. if (!flags) {
  243. *dest = *oci->frame_to_show;
  244. /* handle problem with extending borders */
  245. dest->y_width = oci->Width;
  246. dest->y_height = oci->Height;
  247. dest->uv_height = dest->y_height / 2;
  248. oci->postproc_state.last_base_qindex = oci->base_qindex;
  249. oci->postproc_state.last_frame_valid = 1;
  250. return 0;
  251. }
  252. if (flags & VP8D_ADDNOISE) {
  253. if (!oci->postproc_state.generated_noise) {
  254. oci->postproc_state.generated_noise = vpx_calloc(
  255. oci->Width + 256, sizeof(*oci->postproc_state.generated_noise));
  256. if (!oci->postproc_state.generated_noise) return 1;
  257. }
  258. }
  259. /* Allocate post_proc_buffer_int if needed */
  260. if ((flags & VP8D_MFQE) && !oci->post_proc_buffer_int_used) {
  261. if ((flags & VP8D_DEBLOCK) || (flags & VP8D_DEMACROBLOCK)) {
  262. int width = (oci->Width + 15) & ~15;
  263. int height = (oci->Height + 15) & ~15;
  264. if (vp8_yv12_alloc_frame_buffer(&oci->post_proc_buffer_int, width, height,
  265. VP8BORDERINPIXELS)) {
  266. vpx_internal_error(&oci->error, VPX_CODEC_MEM_ERROR,
  267. "Failed to allocate MFQE framebuffer");
  268. }
  269. oci->post_proc_buffer_int_used = 1;
  270. /* insure that postproc is set to all 0's so that post proc
  271. * doesn't pull random data in from edge
  272. */
  273. memset((&oci->post_proc_buffer_int)->buffer_alloc, 128,
  274. (&oci->post_proc_buffer)->frame_size);
  275. }
  276. }
  277. vp8_clear_system_state();
  278. if ((flags & VP8D_MFQE) && oci->postproc_state.last_frame_valid &&
  279. oci->current_video_frame >= 2 &&
  280. oci->postproc_state.last_base_qindex < 60 &&
  281. oci->base_qindex - oci->postproc_state.last_base_qindex >= 20) {
  282. vp8_multiframe_quality_enhance(oci);
  283. if (((flags & VP8D_DEBLOCK) || (flags & VP8D_DEMACROBLOCK)) &&
  284. oci->post_proc_buffer_int_used) {
  285. vp8_yv12_copy_frame(&oci->post_proc_buffer, &oci->post_proc_buffer_int);
  286. if (flags & VP8D_DEMACROBLOCK) {
  287. vp8_deblock(oci, &oci->post_proc_buffer_int, &oci->post_proc_buffer,
  288. q + (deblock_level - 5) * 10, 1, 0);
  289. vp8_de_mblock(&oci->post_proc_buffer, q + (deblock_level - 5) * 10);
  290. } else if (flags & VP8D_DEBLOCK) {
  291. vp8_deblock(oci, &oci->post_proc_buffer_int, &oci->post_proc_buffer, q,
  292. 1, 0);
  293. }
  294. }
  295. /* Move partially towards the base q of the previous frame */
  296. oci->postproc_state.last_base_qindex =
  297. (3 * oci->postproc_state.last_base_qindex + oci->base_qindex) >> 2;
  298. } else if (flags & VP8D_DEMACROBLOCK) {
  299. vp8_deblock(oci, oci->frame_to_show, &oci->post_proc_buffer,
  300. q + (deblock_level - 5) * 10, 1, 0);
  301. vp8_de_mblock(&oci->post_proc_buffer, q + (deblock_level - 5) * 10);
  302. oci->postproc_state.last_base_qindex = oci->base_qindex;
  303. } else if (flags & VP8D_DEBLOCK) {
  304. vp8_deblock(oci, oci->frame_to_show, &oci->post_proc_buffer, q, 1, 0);
  305. oci->postproc_state.last_base_qindex = oci->base_qindex;
  306. } else {
  307. vp8_yv12_copy_frame(oci->frame_to_show, &oci->post_proc_buffer);
  308. oci->postproc_state.last_base_qindex = oci->base_qindex;
  309. }
  310. oci->postproc_state.last_frame_valid = 1;
  311. if (flags & VP8D_ADDNOISE) {
  312. if (oci->postproc_state.last_q != q ||
  313. oci->postproc_state.last_noise != noise_level) {
  314. double sigma;
  315. struct postproc_state *ppstate = &oci->postproc_state;
  316. vp8_clear_system_state();
  317. sigma = noise_level + .5 + .6 * q / 63.0;
  318. ppstate->clamp =
  319. vpx_setup_noise(sigma, ppstate->generated_noise, oci->Width + 256);
  320. ppstate->last_q = q;
  321. ppstate->last_noise = noise_level;
  322. }
  323. vpx_plane_add_noise(
  324. oci->post_proc_buffer.y_buffer, oci->postproc_state.generated_noise,
  325. oci->postproc_state.clamp, oci->postproc_state.clamp,
  326. oci->post_proc_buffer.y_width, oci->post_proc_buffer.y_height,
  327. oci->post_proc_buffer.y_stride);
  328. }
  329. *dest = oci->post_proc_buffer;
  330. /* handle problem with extending borders */
  331. dest->y_width = oci->Width;
  332. dest->y_height = oci->Height;
  333. dest->uv_height = dest->y_height / 2;
  334. return 0;
  335. }
  336. #endif