vp9_mbgraph.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 <limits.h>
  11. #include "./vp9_rtcd.h"
  12. #include "./vpx_dsp_rtcd.h"
  13. #include "vpx_dsp/vpx_dsp_common.h"
  14. #include "vpx_mem/vpx_mem.h"
  15. #include "vpx_ports/system_state.h"
  16. #include "vp9/encoder/vp9_segmentation.h"
  17. #include "vp9/encoder/vp9_mcomp.h"
  18. #include "vp9/common/vp9_blockd.h"
  19. #include "vp9/common/vp9_reconinter.h"
  20. #include "vp9/common/vp9_reconintra.h"
  21. static unsigned int do_16x16_motion_iteration(VP9_COMP *cpi, const MV *ref_mv,
  22. MV *dst_mv, int mb_row,
  23. int mb_col) {
  24. MACROBLOCK *const x = &cpi->td.mb;
  25. MACROBLOCKD *const xd = &x->e_mbd;
  26. MV_SPEED_FEATURES *const mv_sf = &cpi->sf.mv;
  27. const SEARCH_METHODS old_search_method = mv_sf->search_method;
  28. const vp9_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[BLOCK_16X16];
  29. const MvLimits tmp_mv_limits = x->mv_limits;
  30. MV ref_full;
  31. int cost_list[5];
  32. // Further step/diamond searches as necessary
  33. int step_param = mv_sf->reduce_first_step_size;
  34. step_param = VPXMIN(step_param, MAX_MVSEARCH_STEPS - 2);
  35. vp9_set_mv_search_range(&x->mv_limits, ref_mv);
  36. ref_full.col = ref_mv->col >> 3;
  37. ref_full.row = ref_mv->row >> 3;
  38. mv_sf->search_method = HEX;
  39. vp9_full_pixel_search(cpi, x, BLOCK_16X16, &ref_full, step_param,
  40. cpi->sf.mv.search_method, x->errorperbit,
  41. cond_cost_list(cpi, cost_list), ref_mv, dst_mv, 0, 0);
  42. mv_sf->search_method = old_search_method;
  43. /* restore UMV window */
  44. x->mv_limits = tmp_mv_limits;
  45. // Try sub-pixel MC
  46. // if (bestsme > error_thresh && bestsme < INT_MAX)
  47. {
  48. uint32_t distortion;
  49. uint32_t sse;
  50. // TODO(yunqing): may use higher tap interp filter than 2 taps if needed.
  51. cpi->find_fractional_mv_step(
  52. x, dst_mv, ref_mv, cpi->common.allow_high_precision_mv, x->errorperbit,
  53. &v_fn_ptr, 0, mv_sf->subpel_search_level,
  54. cond_cost_list(cpi, cost_list), NULL, NULL, &distortion, &sse, NULL, 0,
  55. 0, USE_2_TAPS);
  56. }
  57. xd->mi[0]->mode = NEWMV;
  58. xd->mi[0]->mv[0].as_mv = *dst_mv;
  59. vp9_build_inter_predictors_sby(xd, mb_row, mb_col, BLOCK_16X16);
  60. return vpx_sad16x16(x->plane[0].src.buf, x->plane[0].src.stride,
  61. xd->plane[0].dst.buf, xd->plane[0].dst.stride);
  62. }
  63. static int do_16x16_motion_search(VP9_COMP *cpi, const MV *ref_mv,
  64. int_mv *dst_mv, int mb_row, int mb_col) {
  65. MACROBLOCK *const x = &cpi->td.mb;
  66. MACROBLOCKD *const xd = &x->e_mbd;
  67. unsigned int err, tmp_err;
  68. MV tmp_mv;
  69. // Try zero MV first
  70. // FIXME should really use something like near/nearest MV and/or MV prediction
  71. err = vpx_sad16x16(x->plane[0].src.buf, x->plane[0].src.stride,
  72. xd->plane[0].pre[0].buf, xd->plane[0].pre[0].stride);
  73. dst_mv->as_int = 0;
  74. // Test last reference frame using the previous best mv as the
  75. // starting point (best reference) for the search
  76. tmp_err = do_16x16_motion_iteration(cpi, ref_mv, &tmp_mv, mb_row, mb_col);
  77. if (tmp_err < err) {
  78. err = tmp_err;
  79. dst_mv->as_mv = tmp_mv;
  80. }
  81. // If the current best reference mv is not centered on 0,0 then do a 0,0
  82. // based search as well.
  83. if (ref_mv->row != 0 || ref_mv->col != 0) {
  84. unsigned int tmp_err;
  85. MV zero_ref_mv = { 0, 0 }, tmp_mv;
  86. tmp_err =
  87. do_16x16_motion_iteration(cpi, &zero_ref_mv, &tmp_mv, mb_row, mb_col);
  88. if (tmp_err < err) {
  89. dst_mv->as_mv = tmp_mv;
  90. err = tmp_err;
  91. }
  92. }
  93. return err;
  94. }
  95. static int do_16x16_zerozero_search(VP9_COMP *cpi, int_mv *dst_mv) {
  96. MACROBLOCK *const x = &cpi->td.mb;
  97. MACROBLOCKD *const xd = &x->e_mbd;
  98. unsigned int err;
  99. // Try zero MV first
  100. // FIXME should really use something like near/nearest MV and/or MV prediction
  101. err = vpx_sad16x16(x->plane[0].src.buf, x->plane[0].src.stride,
  102. xd->plane[0].pre[0].buf, xd->plane[0].pre[0].stride);
  103. dst_mv->as_int = 0;
  104. return err;
  105. }
  106. static int find_best_16x16_intra(VP9_COMP *cpi, PREDICTION_MODE *pbest_mode) {
  107. MACROBLOCK *const x = &cpi->td.mb;
  108. MACROBLOCKD *const xd = &x->e_mbd;
  109. PREDICTION_MODE best_mode = -1, mode;
  110. unsigned int best_err = INT_MAX;
  111. // calculate SATD for each intra prediction mode;
  112. // we're intentionally not doing 4x4, we just want a rough estimate
  113. for (mode = DC_PRED; mode <= TM_PRED; mode++) {
  114. unsigned int err;
  115. xd->mi[0]->mode = mode;
  116. vp9_predict_intra_block(xd, 2, TX_16X16, mode, x->plane[0].src.buf,
  117. x->plane[0].src.stride, xd->plane[0].dst.buf,
  118. xd->plane[0].dst.stride, 0, 0, 0);
  119. err = vpx_sad16x16(x->plane[0].src.buf, x->plane[0].src.stride,
  120. xd->plane[0].dst.buf, xd->plane[0].dst.stride);
  121. // find best
  122. if (err < best_err) {
  123. best_err = err;
  124. best_mode = mode;
  125. }
  126. }
  127. if (pbest_mode) *pbest_mode = best_mode;
  128. return best_err;
  129. }
  130. static void update_mbgraph_mb_stats(VP9_COMP *cpi, MBGRAPH_MB_STATS *stats,
  131. YV12_BUFFER_CONFIG *buf, int mb_y_offset,
  132. YV12_BUFFER_CONFIG *golden_ref,
  133. const MV *prev_golden_ref_mv,
  134. YV12_BUFFER_CONFIG *alt_ref, int mb_row,
  135. int mb_col) {
  136. MACROBLOCK *const x = &cpi->td.mb;
  137. MACROBLOCKD *const xd = &x->e_mbd;
  138. int intra_error;
  139. VP9_COMMON *cm = &cpi->common;
  140. // FIXME in practice we're completely ignoring chroma here
  141. x->plane[0].src.buf = buf->y_buffer + mb_y_offset;
  142. x->plane[0].src.stride = buf->y_stride;
  143. xd->plane[0].dst.buf = get_frame_new_buffer(cm)->y_buffer + mb_y_offset;
  144. xd->plane[0].dst.stride = get_frame_new_buffer(cm)->y_stride;
  145. // do intra 16x16 prediction
  146. intra_error = find_best_16x16_intra(cpi, &stats->ref[INTRA_FRAME].m.mode);
  147. if (intra_error <= 0) intra_error = 1;
  148. stats->ref[INTRA_FRAME].err = intra_error;
  149. // Golden frame MV search, if it exists and is different than last frame
  150. if (golden_ref) {
  151. int g_motion_error;
  152. xd->plane[0].pre[0].buf = golden_ref->y_buffer + mb_y_offset;
  153. xd->plane[0].pre[0].stride = golden_ref->y_stride;
  154. g_motion_error =
  155. do_16x16_motion_search(cpi, prev_golden_ref_mv,
  156. &stats->ref[GOLDEN_FRAME].m.mv, mb_row, mb_col);
  157. stats->ref[GOLDEN_FRAME].err = g_motion_error;
  158. } else {
  159. stats->ref[GOLDEN_FRAME].err = INT_MAX;
  160. stats->ref[GOLDEN_FRAME].m.mv.as_int = 0;
  161. }
  162. // Do an Alt-ref frame MV search, if it exists and is different than
  163. // last/golden frame.
  164. if (alt_ref) {
  165. int a_motion_error;
  166. xd->plane[0].pre[0].buf = alt_ref->y_buffer + mb_y_offset;
  167. xd->plane[0].pre[0].stride = alt_ref->y_stride;
  168. a_motion_error =
  169. do_16x16_zerozero_search(cpi, &stats->ref[ALTREF_FRAME].m.mv);
  170. stats->ref[ALTREF_FRAME].err = a_motion_error;
  171. } else {
  172. stats->ref[ALTREF_FRAME].err = INT_MAX;
  173. stats->ref[ALTREF_FRAME].m.mv.as_int = 0;
  174. }
  175. }
  176. static void update_mbgraph_frame_stats(VP9_COMP *cpi,
  177. MBGRAPH_FRAME_STATS *stats,
  178. YV12_BUFFER_CONFIG *buf,
  179. YV12_BUFFER_CONFIG *golden_ref,
  180. YV12_BUFFER_CONFIG *alt_ref) {
  181. MACROBLOCK *const x = &cpi->td.mb;
  182. MACROBLOCKD *const xd = &x->e_mbd;
  183. VP9_COMMON *const cm = &cpi->common;
  184. int mb_col, mb_row, offset = 0;
  185. int mb_y_offset = 0, arf_y_offset = 0, gld_y_offset = 0;
  186. MV gld_top_mv = { 0, 0 };
  187. MODE_INFO mi_local;
  188. MODE_INFO mi_above, mi_left;
  189. vp9_zero(mi_local);
  190. // Set up limit values for motion vectors to prevent them extending outside
  191. // the UMV borders.
  192. x->mv_limits.row_min = -BORDER_MV_PIXELS_B16;
  193. x->mv_limits.row_max = (cm->mb_rows - 1) * 8 + BORDER_MV_PIXELS_B16;
  194. // Signal to vp9_predict_intra_block() that above is not available
  195. xd->above_mi = NULL;
  196. xd->plane[0].dst.stride = buf->y_stride;
  197. xd->plane[0].pre[0].stride = buf->y_stride;
  198. xd->plane[1].dst.stride = buf->uv_stride;
  199. xd->mi[0] = &mi_local;
  200. mi_local.sb_type = BLOCK_16X16;
  201. mi_local.ref_frame[0] = LAST_FRAME;
  202. mi_local.ref_frame[1] = NONE;
  203. for (mb_row = 0; mb_row < cm->mb_rows; mb_row++) {
  204. MV gld_left_mv = gld_top_mv;
  205. int mb_y_in_offset = mb_y_offset;
  206. int arf_y_in_offset = arf_y_offset;
  207. int gld_y_in_offset = gld_y_offset;
  208. // Set up limit values for motion vectors to prevent them extending outside
  209. // the UMV borders.
  210. x->mv_limits.col_min = -BORDER_MV_PIXELS_B16;
  211. x->mv_limits.col_max = (cm->mb_cols - 1) * 8 + BORDER_MV_PIXELS_B16;
  212. // Signal to vp9_predict_intra_block() that left is not available
  213. xd->left_mi = NULL;
  214. for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) {
  215. MBGRAPH_MB_STATS *mb_stats = &stats->mb_stats[offset + mb_col];
  216. update_mbgraph_mb_stats(cpi, mb_stats, buf, mb_y_in_offset, golden_ref,
  217. &gld_left_mv, alt_ref, mb_row, mb_col);
  218. gld_left_mv = mb_stats->ref[GOLDEN_FRAME].m.mv.as_mv;
  219. if (mb_col == 0) {
  220. gld_top_mv = gld_left_mv;
  221. }
  222. // Signal to vp9_predict_intra_block() that left is available
  223. xd->left_mi = &mi_left;
  224. mb_y_in_offset += 16;
  225. gld_y_in_offset += 16;
  226. arf_y_in_offset += 16;
  227. x->mv_limits.col_min -= 16;
  228. x->mv_limits.col_max -= 16;
  229. }
  230. // Signal to vp9_predict_intra_block() that above is available
  231. xd->above_mi = &mi_above;
  232. mb_y_offset += buf->y_stride * 16;
  233. gld_y_offset += golden_ref->y_stride * 16;
  234. if (alt_ref) arf_y_offset += alt_ref->y_stride * 16;
  235. x->mv_limits.row_min -= 16;
  236. x->mv_limits.row_max -= 16;
  237. offset += cm->mb_cols;
  238. }
  239. }
  240. // void separate_arf_mbs_byzz
  241. static void separate_arf_mbs(VP9_COMP *cpi) {
  242. VP9_COMMON *const cm = &cpi->common;
  243. int mb_col, mb_row, offset, i;
  244. int mi_row, mi_col;
  245. int ncnt[4] = { 0 };
  246. int n_frames = cpi->mbgraph_n_frames;
  247. int *arf_not_zz;
  248. CHECK_MEM_ERROR(
  249. cm, arf_not_zz,
  250. vpx_calloc(cm->mb_rows * cm->mb_cols * sizeof(*arf_not_zz), 1));
  251. // We are not interested in results beyond the alt ref itself.
  252. if (n_frames > cpi->rc.frames_till_gf_update_due)
  253. n_frames = cpi->rc.frames_till_gf_update_due;
  254. // defer cost to reference frames
  255. for (i = n_frames - 1; i >= 0; i--) {
  256. MBGRAPH_FRAME_STATS *frame_stats = &cpi->mbgraph_stats[i];
  257. for (offset = 0, mb_row = 0; mb_row < cm->mb_rows;
  258. offset += cm->mb_cols, mb_row++) {
  259. for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) {
  260. MBGRAPH_MB_STATS *mb_stats = &frame_stats->mb_stats[offset + mb_col];
  261. int altref_err = mb_stats->ref[ALTREF_FRAME].err;
  262. int intra_err = mb_stats->ref[INTRA_FRAME].err;
  263. int golden_err = mb_stats->ref[GOLDEN_FRAME].err;
  264. // Test for altref vs intra and gf and that its mv was 0,0.
  265. if (altref_err > 1000 || altref_err > intra_err ||
  266. altref_err > golden_err) {
  267. arf_not_zz[offset + mb_col]++;
  268. }
  269. }
  270. }
  271. }
  272. // arf_not_zz is indexed by MB, but this loop is indexed by MI to avoid out
  273. // of bound access in segmentation_map
  274. for (mi_row = 0; mi_row < cm->mi_rows; mi_row++) {
  275. for (mi_col = 0; mi_col < cm->mi_cols; mi_col++) {
  276. // If any of the blocks in the sequence failed then the MB
  277. // goes in segment 0
  278. if (arf_not_zz[mi_row / 2 * cm->mb_cols + mi_col / 2]) {
  279. ncnt[0]++;
  280. cpi->segmentation_map[mi_row * cm->mi_cols + mi_col] = 0;
  281. } else {
  282. cpi->segmentation_map[mi_row * cm->mi_cols + mi_col] = 1;
  283. ncnt[1]++;
  284. }
  285. }
  286. }
  287. // Only bother with segmentation if over 10% of the MBs in static segment
  288. // if ( ncnt[1] && (ncnt[0] / ncnt[1] < 10) )
  289. if (1) {
  290. // Note % of blocks that are marked as static
  291. if (cm->MBs)
  292. cpi->static_mb_pct = (ncnt[1] * 100) / (cm->mi_rows * cm->mi_cols);
  293. // This error case should not be reachable as this function should
  294. // never be called with the common data structure uninitialized.
  295. else
  296. cpi->static_mb_pct = 0;
  297. vp9_enable_segmentation(&cm->seg);
  298. } else {
  299. cpi->static_mb_pct = 0;
  300. vp9_disable_segmentation(&cm->seg);
  301. }
  302. // Free localy allocated storage
  303. vpx_free(arf_not_zz);
  304. }
  305. void vp9_update_mbgraph_stats(VP9_COMP *cpi) {
  306. VP9_COMMON *const cm = &cpi->common;
  307. int i, n_frames = vp9_lookahead_depth(cpi->lookahead);
  308. YV12_BUFFER_CONFIG *golden_ref = get_ref_frame_buffer(cpi, GOLDEN_FRAME);
  309. assert(golden_ref != NULL);
  310. // we need to look ahead beyond where the ARF transitions into
  311. // being a GF - so exit if we don't look ahead beyond that
  312. if (n_frames <= cpi->rc.frames_till_gf_update_due) return;
  313. if (n_frames > MAX_LAG_BUFFERS) n_frames = MAX_LAG_BUFFERS;
  314. cpi->mbgraph_n_frames = n_frames;
  315. for (i = 0; i < n_frames; i++) {
  316. MBGRAPH_FRAME_STATS *frame_stats = &cpi->mbgraph_stats[i];
  317. memset(frame_stats->mb_stats, 0,
  318. cm->mb_rows * cm->mb_cols * sizeof(*cpi->mbgraph_stats[i].mb_stats));
  319. }
  320. // do motion search to find contribution of each reference to data
  321. // later on in this GF group
  322. // FIXME really, the GF/last MC search should be done forward, and
  323. // the ARF MC search backwards, to get optimal results for MV caching
  324. for (i = 0; i < n_frames; i++) {
  325. MBGRAPH_FRAME_STATS *frame_stats = &cpi->mbgraph_stats[i];
  326. struct lookahead_entry *q_cur = vp9_lookahead_peek(cpi->lookahead, i);
  327. assert(q_cur != NULL);
  328. update_mbgraph_frame_stats(cpi, frame_stats, &q_cur->img, golden_ref,
  329. cpi->Source);
  330. }
  331. vpx_clear_system_state();
  332. separate_arf_mbs(cpi);
  333. }