mfqe.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Copyright (c) 2012 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. /* MFQE: Multiframe Quality Enhancement
  11. * In rate limited situations keyframes may cause significant visual artifacts
  12. * commonly referred to as "popping." This file implements a postproccesing
  13. * algorithm which blends data from the preceeding frame when there is no
  14. * motion and the q from the previous frame is lower which indicates that it is
  15. * higher quality.
  16. */
  17. #include "./vp8_rtcd.h"
  18. #include "./vpx_dsp_rtcd.h"
  19. #include "vp8/common/common.h"
  20. #include "vp8/common/postproc.h"
  21. #include "vpx_dsp/variance.h"
  22. #include "vpx_mem/vpx_mem.h"
  23. #include "vpx_scale/yv12config.h"
  24. #include <limits.h>
  25. #include <stdlib.h>
  26. static void filter_by_weight(unsigned char *src, int src_stride,
  27. unsigned char *dst, int dst_stride, int block_size,
  28. int src_weight) {
  29. int dst_weight = (1 << MFQE_PRECISION) - src_weight;
  30. int rounding_bit = 1 << (MFQE_PRECISION - 1);
  31. int r, c;
  32. for (r = 0; r < block_size; ++r) {
  33. for (c = 0; c < block_size; ++c) {
  34. dst[c] = (src[c] * src_weight + dst[c] * dst_weight + rounding_bit) >>
  35. MFQE_PRECISION;
  36. }
  37. src += src_stride;
  38. dst += dst_stride;
  39. }
  40. }
  41. void vp8_filter_by_weight16x16_c(unsigned char *src, int src_stride,
  42. unsigned char *dst, int dst_stride,
  43. int src_weight) {
  44. filter_by_weight(src, src_stride, dst, dst_stride, 16, src_weight);
  45. }
  46. void vp8_filter_by_weight8x8_c(unsigned char *src, int src_stride,
  47. unsigned char *dst, int dst_stride,
  48. int src_weight) {
  49. filter_by_weight(src, src_stride, dst, dst_stride, 8, src_weight);
  50. }
  51. void vp8_filter_by_weight4x4_c(unsigned char *src, int src_stride,
  52. unsigned char *dst, int dst_stride,
  53. int src_weight) {
  54. filter_by_weight(src, src_stride, dst, dst_stride, 4, src_weight);
  55. }
  56. static void apply_ifactor(unsigned char *y_src, int y_src_stride,
  57. unsigned char *y_dst, int y_dst_stride,
  58. unsigned char *u_src, unsigned char *v_src,
  59. int uv_src_stride, unsigned char *u_dst,
  60. unsigned char *v_dst, int uv_dst_stride,
  61. int block_size, int src_weight) {
  62. if (block_size == 16) {
  63. vp8_filter_by_weight16x16(y_src, y_src_stride, y_dst, y_dst_stride,
  64. src_weight);
  65. vp8_filter_by_weight8x8(u_src, uv_src_stride, u_dst, uv_dst_stride,
  66. src_weight);
  67. vp8_filter_by_weight8x8(v_src, uv_src_stride, v_dst, uv_dst_stride,
  68. src_weight);
  69. } else {
  70. vp8_filter_by_weight8x8(y_src, y_src_stride, y_dst, y_dst_stride,
  71. src_weight);
  72. vp8_filter_by_weight4x4(u_src, uv_src_stride, u_dst, uv_dst_stride,
  73. src_weight);
  74. vp8_filter_by_weight4x4(v_src, uv_src_stride, v_dst, uv_dst_stride,
  75. src_weight);
  76. }
  77. }
  78. static unsigned int int_sqrt(unsigned int x) {
  79. unsigned int y = x;
  80. unsigned int guess;
  81. int p = 1;
  82. while (y >>= 1) p++;
  83. p >>= 1;
  84. guess = 0;
  85. while (p >= 0) {
  86. guess |= (1 << p);
  87. if (x < guess * guess) guess -= (1 << p);
  88. p--;
  89. }
  90. /* choose between guess or guess+1 */
  91. return guess + (guess * guess + guess + 1 <= x);
  92. }
  93. #define USE_SSD
  94. static void multiframe_quality_enhance_block(
  95. int blksize, /* Currently only values supported are 16, 8 */
  96. int qcurr, int qprev, unsigned char *y, unsigned char *u, unsigned char *v,
  97. int y_stride, int uv_stride, unsigned char *yd, unsigned char *ud,
  98. unsigned char *vd, int yd_stride, int uvd_stride) {
  99. static const unsigned char VP8_ZEROS[16] = { 0, 0, 0, 0, 0, 0, 0, 0,
  100. 0, 0, 0, 0, 0, 0, 0, 0 };
  101. int uvblksize = blksize >> 1;
  102. int qdiff = qcurr - qprev;
  103. int i;
  104. unsigned char *up;
  105. unsigned char *udp;
  106. unsigned char *vp;
  107. unsigned char *vdp;
  108. unsigned int act, actd, sad, usad, vsad, sse, thr, thrsq, actrisk;
  109. if (blksize == 16) {
  110. actd = (vpx_variance16x16(yd, yd_stride, VP8_ZEROS, 0, &sse) + 128) >> 8;
  111. act = (vpx_variance16x16(y, y_stride, VP8_ZEROS, 0, &sse) + 128) >> 8;
  112. #ifdef USE_SSD
  113. vpx_variance16x16(y, y_stride, yd, yd_stride, &sse);
  114. sad = (sse + 128) >> 8;
  115. vpx_variance8x8(u, uv_stride, ud, uvd_stride, &sse);
  116. usad = (sse + 32) >> 6;
  117. vpx_variance8x8(v, uv_stride, vd, uvd_stride, &sse);
  118. vsad = (sse + 32) >> 6;
  119. #else
  120. sad = (vpx_sad16x16(y, y_stride, yd, yd_stride) + 128) >> 8;
  121. usad = (vpx_sad8x8(u, uv_stride, ud, uvd_stride) + 32) >> 6;
  122. vsad = (vpx_sad8x8(v, uv_stride, vd, uvd_stride) + 32) >> 6;
  123. #endif
  124. } else {
  125. actd = (vpx_variance8x8(yd, yd_stride, VP8_ZEROS, 0, &sse) + 32) >> 6;
  126. act = (vpx_variance8x8(y, y_stride, VP8_ZEROS, 0, &sse) + 32) >> 6;
  127. #ifdef USE_SSD
  128. vpx_variance8x8(y, y_stride, yd, yd_stride, &sse);
  129. sad = (sse + 32) >> 6;
  130. vpx_variance4x4(u, uv_stride, ud, uvd_stride, &sse);
  131. usad = (sse + 8) >> 4;
  132. vpx_variance4x4(v, uv_stride, vd, uvd_stride, &sse);
  133. vsad = (sse + 8) >> 4;
  134. #else
  135. sad = (vpx_sad8x8(y, y_stride, yd, yd_stride) + 32) >> 6;
  136. usad = (vpx_sad4x4(u, uv_stride, ud, uvd_stride) + 8) >> 4;
  137. vsad = (vpx_sad4x4(v, uv_stride, vd, uvd_stride) + 8) >> 4;
  138. #endif
  139. }
  140. actrisk = (actd > act * 5);
  141. /* thr = qdiff/16 + log2(act) + log4(qprev) */
  142. thr = (qdiff >> 4);
  143. while (actd >>= 1) thr++;
  144. while (qprev >>= 2) thr++;
  145. #ifdef USE_SSD
  146. thrsq = thr * thr;
  147. if (sad < thrsq &&
  148. /* additional checks for color mismatch and excessive addition of
  149. * high-frequencies */
  150. 4 * usad < thrsq && 4 * vsad < thrsq && !actrisk)
  151. #else
  152. if (sad < thr &&
  153. /* additional checks for color mismatch and excessive addition of
  154. * high-frequencies */
  155. 2 * usad < thr && 2 * vsad < thr && !actrisk)
  156. #endif
  157. {
  158. int ifactor;
  159. #ifdef USE_SSD
  160. /* TODO: optimize this later to not need sqr root */
  161. sad = int_sqrt(sad);
  162. #endif
  163. ifactor = (sad << MFQE_PRECISION) / thr;
  164. ifactor >>= (qdiff >> 5);
  165. if (ifactor) {
  166. apply_ifactor(y, y_stride, yd, yd_stride, u, v, uv_stride, ud, vd,
  167. uvd_stride, blksize, ifactor);
  168. }
  169. } else { /* else implicitly copy from previous frame */
  170. if (blksize == 16) {
  171. vp8_copy_mem16x16(y, y_stride, yd, yd_stride);
  172. vp8_copy_mem8x8(u, uv_stride, ud, uvd_stride);
  173. vp8_copy_mem8x8(v, uv_stride, vd, uvd_stride);
  174. } else {
  175. vp8_copy_mem8x8(y, y_stride, yd, yd_stride);
  176. for (up = u, udp = ud, i = 0; i < uvblksize;
  177. ++i, up += uv_stride, udp += uvd_stride) {
  178. memcpy(udp, up, uvblksize);
  179. }
  180. for (vp = v, vdp = vd, i = 0; i < uvblksize;
  181. ++i, vp += uv_stride, vdp += uvd_stride) {
  182. memcpy(vdp, vp, uvblksize);
  183. }
  184. }
  185. }
  186. }
  187. static int qualify_inter_mb(const MODE_INFO *mode_info_context, int *map) {
  188. if (mode_info_context->mbmi.mb_skip_coeff) {
  189. map[0] = map[1] = map[2] = map[3] = 1;
  190. } else if (mode_info_context->mbmi.mode == SPLITMV) {
  191. static int ndx[4][4] = {
  192. { 0, 1, 4, 5 }, { 2, 3, 6, 7 }, { 8, 9, 12, 13 }, { 10, 11, 14, 15 }
  193. };
  194. int i, j;
  195. vp8_zero(*map);
  196. for (i = 0; i < 4; ++i) {
  197. map[i] = 1;
  198. for (j = 0; j < 4 && map[j]; ++j) {
  199. map[i] &= (mode_info_context->bmi[ndx[i][j]].mv.as_mv.row <= 2 &&
  200. mode_info_context->bmi[ndx[i][j]].mv.as_mv.col <= 2);
  201. }
  202. }
  203. } else {
  204. map[0] = map[1] = map[2] = map[3] =
  205. (mode_info_context->mbmi.mode > B_PRED &&
  206. abs(mode_info_context->mbmi.mv.as_mv.row) <= 2 &&
  207. abs(mode_info_context->mbmi.mv.as_mv.col) <= 2);
  208. }
  209. return (map[0] + map[1] + map[2] + map[3]);
  210. }
  211. void vp8_multiframe_quality_enhance(VP8_COMMON *cm) {
  212. YV12_BUFFER_CONFIG *show = cm->frame_to_show;
  213. YV12_BUFFER_CONFIG *dest = &cm->post_proc_buffer;
  214. FRAME_TYPE frame_type = cm->frame_type;
  215. /* Point at base of Mb MODE_INFO list has motion vectors etc */
  216. const MODE_INFO *mode_info_context = cm->mi;
  217. int mb_row;
  218. int mb_col;
  219. int totmap, map[4];
  220. int qcurr = cm->base_qindex;
  221. int qprev = cm->postproc_state.last_base_qindex;
  222. unsigned char *y_ptr, *u_ptr, *v_ptr;
  223. unsigned char *yd_ptr, *ud_ptr, *vd_ptr;
  224. /* Set up the buffer pointers */
  225. y_ptr = show->y_buffer;
  226. u_ptr = show->u_buffer;
  227. v_ptr = show->v_buffer;
  228. yd_ptr = dest->y_buffer;
  229. ud_ptr = dest->u_buffer;
  230. vd_ptr = dest->v_buffer;
  231. /* postprocess each macro block */
  232. for (mb_row = 0; mb_row < cm->mb_rows; ++mb_row) {
  233. for (mb_col = 0; mb_col < cm->mb_cols; ++mb_col) {
  234. /* if motion is high there will likely be no benefit */
  235. if (frame_type == INTER_FRAME) {
  236. totmap = qualify_inter_mb(mode_info_context, map);
  237. } else {
  238. totmap = (frame_type == KEY_FRAME ? 4 : 0);
  239. }
  240. if (totmap) {
  241. if (totmap < 4) {
  242. int i, j;
  243. for (i = 0; i < 2; ++i) {
  244. for (j = 0; j < 2; ++j) {
  245. if (map[i * 2 + j]) {
  246. multiframe_quality_enhance_block(
  247. 8, qcurr, qprev, y_ptr + 8 * (i * show->y_stride + j),
  248. u_ptr + 4 * (i * show->uv_stride + j),
  249. v_ptr + 4 * (i * show->uv_stride + j), show->y_stride,
  250. show->uv_stride, yd_ptr + 8 * (i * dest->y_stride + j),
  251. ud_ptr + 4 * (i * dest->uv_stride + j),
  252. vd_ptr + 4 * (i * dest->uv_stride + j), dest->y_stride,
  253. dest->uv_stride);
  254. } else {
  255. /* copy a 8x8 block */
  256. int k;
  257. unsigned char *up = u_ptr + 4 * (i * show->uv_stride + j);
  258. unsigned char *udp = ud_ptr + 4 * (i * dest->uv_stride + j);
  259. unsigned char *vp = v_ptr + 4 * (i * show->uv_stride + j);
  260. unsigned char *vdp = vd_ptr + 4 * (i * dest->uv_stride + j);
  261. vp8_copy_mem8x8(
  262. y_ptr + 8 * (i * show->y_stride + j), show->y_stride,
  263. yd_ptr + 8 * (i * dest->y_stride + j), dest->y_stride);
  264. for (k = 0; k < 4; ++k, up += show->uv_stride,
  265. udp += dest->uv_stride, vp += show->uv_stride,
  266. vdp += dest->uv_stride) {
  267. memcpy(udp, up, 4);
  268. memcpy(vdp, vp, 4);
  269. }
  270. }
  271. }
  272. }
  273. } else { /* totmap = 4 */
  274. multiframe_quality_enhance_block(
  275. 16, qcurr, qprev, y_ptr, u_ptr, v_ptr, show->y_stride,
  276. show->uv_stride, yd_ptr, ud_ptr, vd_ptr, dest->y_stride,
  277. dest->uv_stride);
  278. }
  279. } else {
  280. vp8_copy_mem16x16(y_ptr, show->y_stride, yd_ptr, dest->y_stride);
  281. vp8_copy_mem8x8(u_ptr, show->uv_stride, ud_ptr, dest->uv_stride);
  282. vp8_copy_mem8x8(v_ptr, show->uv_stride, vd_ptr, dest->uv_stride);
  283. }
  284. y_ptr += 16;
  285. u_ptr += 8;
  286. v_ptr += 8;
  287. yd_ptr += 16;
  288. ud_ptr += 8;
  289. vd_ptr += 8;
  290. mode_info_context++; /* step to next MB */
  291. }
  292. y_ptr += show->y_stride * 16 - 16 * cm->mb_cols;
  293. u_ptr += show->uv_stride * 8 - 8 * cm->mb_cols;
  294. v_ptr += show->uv_stride * 8 - 8 * cm->mb_cols;
  295. yd_ptr += dest->y_stride * 16 - 16 * cm->mb_cols;
  296. ud_ptr += dest->uv_stride * 8 - 8 * cm->mb_cols;
  297. vd_ptr += dest->uv_stride * 8 - 8 * cm->mb_cols;
  298. mode_info_context++; /* Skip border mb */
  299. }
  300. }